#include<cstdint>constexprstd::uint8_tmask0{0b0000'0001};// represents bit 0constexprstd::uint8_tmask1{0b0000'0010};// represents bit 1constexprstd::uint8_tmask2{0b0000'0100};// represents bit 2constexprstd::uint8_tmask3{0b0000'1000};// represents bit 3constexprstd::uint8_tmask4{0b0001'0000};// represents bit 4constexprstd::uint8_tmask5{0b0010'0000};// represents bit 5constexprstd::uint8_tmask6{0b0100'0000};// represents bit 6constexprstd::uint8_tmask7{0b1000'0000};// represents bit 7
constexprstd::uint8_tmask0{0x01};// hex for 0000 0001constexprstd::uint8_tmask1{0x02};// hex for 0000 0010constexprstd::uint8_tmask2{0x04};// hex for 0000 0100constexprstd::uint8_tmask3{0x08};// hex for 0000 1000constexprstd::uint8_tmask4{0x10};// hex for 0001 0000constexprstd::uint8_tmask5{0x20};// hex for 0010 0000constexprstd::uint8_tmask6{0x40};// hex for 0100 0000constexprstd::uint8_tmask7{0x80};// hex for 1000 0000
#include<cstdint>#include<iostream>intmain(){constexprstd::uint8_tmask0{0b0000'0001};// represents bit 0constexprstd::uint8_tmask1{0b0000'0010};// represents bit 1constexprstd::uint8_tmask2{0b0000'0100};// represents bit 2constexprstd::uint8_tmask3{0b0000'1000};// represents bit 3constexprstd::uint8_tmask4{0b0001'0000};// represents bit 4constexprstd::uint8_tmask5{0b0010'0000};// represents bit 5constexprstd::uint8_tmask6{0b0100'0000};// represents bit 6constexprstd::uint8_tmask7{0b1000'0000};// represents bit 7std::uint8_tflags{0b0000'0101};// 8 bits in size means room for 8 flagsstd::cout<<"bit 0 is "<<((flags&mask0)?"on\n":"off\n");std::cout<<"bit 1 is "<<((flags&mask1)?"on\n":"off\n");return0;}
这打印:
12
bit 1 is off
bit 1 is on
置1一位
要设置(置1)位,我们将按位或等于(运算符 |=)与相应位的位掩码结合使用:
1 2 3 4 5 6 7 8 9101112131415161718192021222324
#include<cstdint>#include<iostream>intmain(){constexprstd::uint8_tmask0{0b0000'0001};// represents bit 0constexprstd::uint8_tmask1{0b0000'0010};// represents bit 1constexprstd::uint8_tmask2{0b0000'0100};// represents bit 2constexprstd::uint8_tmask3{0b0000'1000};// represents bit 3constexprstd::uint8_tmask4{0b0001'0000};// represents bit 4constexprstd::uint8_tmask5{0b0010'0000};// represents bit 5constexprstd::uint8_tmask6{0b0100'0000};// represents bit 6constexprstd::uint8_tmask7{0b1000'0000};// represents bit 7std::uint8_tflags{0b0000'0101};// 8 bits in size means room for 8 flagsstd::cout<<"bit 1 is "<<((flags&mask1)?"on\n":"off\n");flags|=mask1;// turn on bit 1std::cout<<"bit 1 is "<<((flags&mask1)?"on\n":"off\n");return0;}
这打印:
12
bit 1 is off
bit 1 is on
我们还可以使用按位或同时置1多个位:
1
flags|=(mask4|mask5);// turn bits 4 and 5 on at the same time
置0一位
要清除位(置0),我们同时使用按位与和按位非:
1 2 3 4 5 6 7 8 9101112131415161718192021222324
#include<cstdint>#include<iostream>intmain(){constexprstd::uint8_tmask0{0b0000'0001};// represents bit 0constexprstd::uint8_tmask1{0b0000'0010};// represents bit 1constexprstd::uint8_tmask2{0b0000'0100};// represents bit 2constexprstd::uint8_tmask3{0b0000'1000};// represents bit 3constexprstd::uint8_tmask4{0b0001'0000};// represents bit 4constexprstd::uint8_tmask5{0b0010'0000};// represents bit 5constexprstd::uint8_tmask6{0b0100'0000};// represents bit 6constexprstd::uint8_tmask7{0b1000'0000};// represents bit 7std::uint8_tflags{0b0000'0101};// 8 bits in size means room for 8 flagsstd::cout<<"bit 2 is "<<((flags&mask2)?"on\n":"off\n");flags&=~mask2;// turn off bit 2std::cout<<"bit 2 is "<<((flags&mask2)?"on\n":"off\n");return0;}
这打印:
12
bit 2 is on
bit 2 is off
我们可以同时关闭多个位:
1
flags&=~(mask4|mask5);// turn bits 4 and 5 off at the same time
翻转一位
要切换位状态,我们使用按位异或:
1 2 3 4 5 6 7 8 9101112131415161718192021222324
#include<cstdint>#include<iostream>intmain(){constexprstd::uint8_tmask0{0b0000'0001};// represents bit 0constexprstd::uint8_tmask1{0b0000'0010};// represents bit 1constexprstd::uint8_tmask2{0b0000'0100};// represents bit 2constexprstd::uint8_tmask3{0b0000'1000};// represents bit 3constexprstd::uint8_tmask4{0b0001'0000};// represents bit 4constexprstd::uint8_tmask5{0b0010'0000};// represents bit 5constexprstd::uint8_tmask6{0b0100'0000};// represents bit 6constexprstd::uint8_tmask7{0b1000'0000};// represents bit 7std::uint8_tflags{0b0000'0101};// 8 bits in size means room for 8 flagsstd::cout<<"bit 2 is "<<((flags&mask2)?"on\n":"off\n");flags^=mask2;// flip bit 2std::cout<<"bit 2 is "<<((flags&mask2)?"on\n":"off\n");flags^=mask2;// flip bit 2std::cout<<"bit 2 is "<<((flags&mask2)?"on\n":"off\n");return0;}
这打印:
123
bit 2 is on
bit 2 is off
bit 2 is on
我们可以同时翻转多个位:
1
flags^=(mask4|mask5);// flip bits 4 and 5 at the same time
#include<cstdint>#include<iostream>#include<bitset>intmain(){constexprstd::bitset<8>mask0{0b0000'0001};// represents bit 0constexprstd::bitset<8>mask1{0b0000'0010};// represents bit 1constexprstd::bitset<8>mask2{0b0000'0100};// represents bit 2constexprstd::bitset<8>mask3{0b0000'1000};// represents bit 3constexprstd::bitset<8>mask4{0b0001'0000};// represents bit 4constexprstd::bitset<8>mask5{0b0010'0000};// represents bit 5constexprstd::bitset<8>mask6{0b0100'0000};// represents bit 6constexprstd::bitset<8>mask7{0b1000'0000};// represents bit 7std::bitset<8>flags{0b0000'0101};// 8 bits in size means room for 8 flagsstd::cout<<"bit 1 is "<<(flags.test(1)?"on\n":"off\n");std::cout<<"bit 2 is "<<(flags.test(2)?"on\n":"off\n");flags^=(mask1|mask2);// flip bits 1 and 2std::cout<<"bit 1 is "<<(flags.test(1)?"on\n":"off\n");std::cout<<"bit 2 is "<<(flags.test(2)?"on\n":"off\n");flags|=(mask1|mask2);// turn bits 1 and 2 onstd::cout<<"bit 1 is "<<(flags.test(1)?"on\n":"off\n");std::cout<<"bit 2 is "<<(flags.test(2)?"on\n":"off\n");flags&=~(mask1|mask2);// turn bits 1 and 2 offstd::cout<<"bit 1 is "<<(flags.test(1)?"on\n":"off\n");std::cout<<"bit 2 is "<<(flags.test(2)?"on\n":"off\n");return0;}
这打印:
12345678
bit 1 is off
bit 2 is on
bit 1 is on
bit 2 is off
bit 1 is on
bit 2 is on
bit 1 is off
bit 2 is off
#include<cstdint>#include<iostream>intmain(){// Define a bunch of physical/emotional statesconstexprstd::uint8_tisHungry{1<<0};// 0000 0001constexprstd::uint8_tisSad{1<<1};// 0000 0010constexprstd::uint8_tisMad{1<<2};// 0000 0100constexprstd::uint8_tisHappy{1<<3};// 0000 1000constexprstd::uint8_tisLaughing{1<<4};// 0001 0000constexprstd::uint8_tisAsleep{1<<5};// 0010 0000constexprstd::uint8_tisDead{1<<6};// 0100 0000constexprstd::uint8_tisCrying{1<<7};// 1000 0000std::uint8_tme{};// all flags/options turned off to startme|=(isHappy|isLaughing);// I am happy and laughingme&=~isLaughing;// I am no longer laughing// Query a few states// (we'll use static_cast<bool> to interpret the results as a boolean value)std::cout<<"I am happy? "<<static_cast<bool>(me&isHappy)<<'\n';std::cout<<"I am laughing? "<<static_cast<bool>(me&isLaughing)<<'\n';return0;}
#include<iostream>#include<bitset>intmain(){// Define a bunch of physical/emotional statesconstexprstd::bitset<8>isHungry{0b0000'0001};constexprstd::bitset<8>isSad{0b0000'0010};constexprstd::bitset<8>isMad{0b0000'0100};constexprstd::bitset<8>isHappy{0b0000'1000};constexprstd::bitset<8>isLaughing{0b0001'0000};constexprstd::bitset<8>isAsleep{0b0010'0000};constexprstd::bitset<8>isDead{0b0100'0000};constexprstd::bitset<8>isCrying{0b1000'0000};std::bitset<8>me{};// all flags/options turned off to startme|=(isHappy|isLaughing);// I am happy and laughingme&=~isLaughing;// I am no longer laughing// Query a few states (we use the any() function to see if any bits remain set)std::cout<<"I am happy? "<<(me&isHappy).any()<<'\n';std::cout<<"I am laughing? "<<(me&isLaughing).any()<<'\n';return0;}
#include<cstdint>#include<iostream>intmain(){constexprstd::uint32_tredBits{0xFF000000};constexprstd::uint32_tgreenBits{0x00FF0000};constexprstd::uint32_tblueBits{0x0000FF00};constexprstd::uint32_talphaBits{0x000000FF};std::cout<<"Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): ";std::uint32_tpixel{};std::cin>>std::hex>>pixel;// std::hex allows us to read in a hex value// use Bitwise AND to isolate red pixels,// then right shift the value into the lower 8 bitsstd::uint8_tred{static_cast<std::uint8_t>((pixel&redBits)>>24)};std::uint8_tgreen{static_cast<std::uint8_t>((pixel&greenBits)>>16)};std::uint8_tblue{static_cast<std::uint8_t>((pixel&blueBits)>>8)};std::uint8_talpha{static_cast<std::uint8_t>(pixel&alphaBits)};std::cout<<"Your color contains:\n";std::cout<<std::hex;// print the following values in hexstd::cout<<static_cast<int>(red)<<" red\n";std::cout<<static_cast<int>(green)<<" green\n";std::cout<<static_cast<int>(blue)<<" blue\n";std::cout<<static_cast<int>(alpha)<<" alpha\n";return0;}
这会产生输出:
123456
Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300
Your color contains:
ff red
7f green
33 blue
0 alpha