BITWISE OPERATIONS

AND

input1 & input2

OR

input1 | input 2

XOR

input1 ^ input2

NOT

~input1

BIT SHIFT LEFT

think of this as multiplying by 10. In base 2, shifting a value one bit to the left corresponds to multiplying it by 2

12345 x 10 = 12345[0]

 * this shifts all digits to the left then filling the gap with a 0
 
variable << bits

 * the left argument (bits) is an integer whose bits are shifted and determines the SIZE fo the shift

BIT SHIFT RIGHT

think of this as dividing by 10. In base 2, shifting one bit to the right corresponds to dividing by 2

12340 / 10 = 1234[]

 * this shifts all digits to the right then dropping the right-most bit []
 
variable >> bits

 *  * the left argument (bits) is an integer whose bits are shifted and determines the SIZE fo the shift

DETECTING BIT STATE

if (flagRegister & mask)
  puts("bit is set");
else
  puts(bit is not set");

RESETTING BIT

flagRegister &= ~mask;

 * 1111111111111111111111111111110111

SETTING BIT

flagRegister = flagResiter | mask;

x | 1 = 1

x | 0 = x

NEGATING BIT

FlagRegister = FlagRegister ^ TheMask;


x ^ 1 = !x

x ^ 0 = x

Last updated