#include <iostream> int main() { unsigned int flags = 482; unsigned int masks[2]={5,6}; std::cout << flags << std::endl; std::cout << "Before:"; for (int i = 128; i > 0; i = i / 2) { if (flags&i) { std::cout << "1"; } else { std::cout << "0"; } } std::cout << std::endl; for (int i = 0; i < 2; ++i) { flags &= ~masks[i]; } std::cout << flags << std::endl; std::cout << "After:"; for (int i = 128; i > 0; i = i / 2) { if (flags&i) { std::cout << "1"; } else { std::cout << "0"; } } std::cout << std::endl; std::cin.get(); } 

It works somehow wrong ... 482 Before: 11100010 480 After: 11100000

  • Actually, 482 per byte does not fit :) - Harry
  • subtracting from the original number 48 ( flags -= 48 ), just "turning off 5 and 6 bits" - dreamIIx

2 answers 2

These are bit positions: unsigned int masks[2]={5,6};

And here these numbers are used directly by flags &= ~masks[i];

 int mask = 0; for (int i = 0; i < 2; ++i) { flags &= ~(1 << masks[i]); } 

    All you need is to build a mask 10011111. Or do it with your hands

     unsigned char mask = 159; 

    or calculate:

     unsigned char mask = 3; mask = ~(mask << 5); 

    Next to reset the 5 and 6 bits with byte byte do

     byte &= mask; 
    • You can generally 0b10011111 . :) - HolyBlackCat
    • @HolyBlackCat You can, of course ... - Harry
    • @Harry, hmm, maybe 5 and 6 will not be so - 0b1100'1111 , and the offset will be not by 5, but by 4? - dreamIIx
    • @dreamIIx Well, actually I’m used to thinking that the bits are numbered from zero to seventh ... - Harry
    • @Harry, oh, well, since you are used to - dreamIIx