Bitwise Operations in C/C++

Noah11012 - Nov 24 '18 - - Dev Community

Maybe you've stumbled across a perplexing and scary looking line of code:

((value << 2) & 32)

You don't know what this is and after some time scouring the internet for answers you come across what is known as bitwise operations. In this post, we'll go over all the bitwise operations possible in C and C++.

Bitwise operations are operations that operate on bit(s). If a bit is 1 then it is said to be "true" or "on". If it is a 0, then it is "false" or "off". Just like arithmetic operators, bitwise operators give a result when the computation is done.

AND

For one bit values, the AND operator gives the result of "on" if both bits are in the "on" state. Otherwise, it produces the value of "off".

1 & 1 == 1
1 & 0 == 0
0 & 1 == 0
0 & 0 == 0

For bytes, it goes over each bit and applies the AND operation individually.

10000000 & 01111111 == 00000000
11000000 & 11100011 == 11000000
10100110 & 10000111 == 10000110
10101010 & 11011011 == 10001010

OR

Denoted by | in C and C++, this operation produces the state of "on" if any of the bit is in the "on" state.

1 | 1 == 1
1 | 0 == 1
0 | 1 == 1
0 | 0 == 0

Just like with the AND operator, the same thing is done with multi-bit values.

XOR

Denoted by ^ it is short for exclusive OR. This produces the "on" state if only ONE of the bits is true.

1 ^ 1 == 0
1 ^ 0 == 1
0 ^ 1 == 1
0 ^ 0 == 0

Operations applied to multi-bit values like bytes are done at the bit level just like with AND and OR.

NEG

Negates the state of a bit. The symbol ~ is used to denote this operation.

~1 == 0
~0 == 1

Same with multi-bit values.

Bitmasks

This isn't an operation but something programmers use sometimes when dealing with bitwise operations. You can think of a bitmask as a filter to extract bits from a multi-bit value.

Say we have the byte 10100111 and we want to get the first two bits. We can use a bitmask to accomplish this goal. A bit of 0 in bitmask means to ignore and 1 means to extract. In our case, the bitmask would like this: 000000011. To apply the bitmask, we use the AND operator.

10100111 & 00000011 == 00000011

Shift operators

You've seen these before with << and >>. They mean "shift to the left" and "shift to the right", respectively.

The operand on the left side is the value being shifted while the operand on the right side is the shift amount.

Unlike the other operators we have looked at, these only work on multi-bit values like bytes.

For demonstration purposes, we will have the value 11001011 to play around with.

These operators shift each bit to the direction specified by the type of shifting operator used by a certain amount. Bits that are shifted out of the byte completely are discarded and padded with a zero on the opposite side.

11001011 << 1 == 10010110
11001011 >> 2 == 00110010

This was a quick article I decided to do after my main Using SDL2 post was released.

Edit:

Thanks to @ccmg for the corrections.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player