Are bitwise operations going to help me to serialize some bools?

别来无恙 提交于 2019-12-05 20:56:36

To store bools in a byte:

bool flag; // value to store
unsigned char b = 0; // all false
int position; // ranges from 0..7
b = b | (flag << position);

To read it back:

flag = (b & (1 << position));

The easy way is to use std::bitset which allows you to use indexing to access individual bits (bools), then get the resulting value as an integer. It also allows the reverse.

int main() {
  std::bitset<8> s;
  s[1] = s[2] = true;  // 0b_0000_0110
  cout << s.to_ulong() << '\n';
}

Without wrapping in fancy template/pre-processor machinery:

  • Set bit 3 in var:
    var |= (1 << 3)
  • Set bit n in var:
    var |= (1 << n)
  • Clear bit n in var:
    var &= ~(1 << n)
  • Test bit n in var: (the !! ensures the result is 0 or 1)
    !!(var & (1 << n))

Try reading this in order.

  1. http://www.cprogramming.com/tutorial/bitwise_operators.html

  2. http://www-graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching

Some people willthink that 2nd link is way too hardcore, but once you will master simple manipulation, it will come handy.

Basic stuff first:

  • The only combination of bits that means false is 00000000 all the others mean true i.e: 00001000,01010101
  • 00000000 = 0(decimal), 00000001 = 2^0, 00000010 = 2^1, 00000100 = 2^2, …. ,10000000 = 2^7
  • There is a big difference between the operands (&&, ||) and (&,|) the first ones give the result of the logic operation between the two numbers, for example:

    00000000 && 00000000 = false,

    01010101 && 10101010 = true

    00001100 || 00000000 = true,

    00000000 || 00000000 = false

    The second pair makes a bitwise operation (the logic operation between each bit of the numbers):

    00000000 & 00000000 = 00000000 = false

    00001111 & 11110000 = 00000000 = false

    01010101 & 10101001 = 00000001 = true

    00001111 | 11110000 = 11111111 = true

    00001100 | 00000011 = 00001111 = true


To work with this and play with the bits, you only need to know some basic tricks:

  • To set a bit to 1 you make the operation | with an octet that has a 1 in that position and ceros in the rest.

For example: we want the first bit of the octet A to be 1 we make: A|00000001

  • To set a bit to 0 you make the operation & with an octet that has a 0 in that position and ones in the rest.

For example: we want the last bit of the octet A to be 0 we make: A&01111111

  • To get the Boolean value that holds a bit you make the operation & with an octet that has a 1 in that position and ceros in the rest.

For example: we want to see the value of the third bit of the octet A, we make: A&00000100, if A was XXXXX1XX we get 00000100 = true and if A was XXXXX0XX we get 00000000 = false;

You can always serialize bitfields. Something like:

struct bools 
{
    bool a:1;
    bool b:1;
    bool c:1;
    bool d:1;
};

has a sizeof 1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!