Bitwise Operations

Bitwise Operations are the fundamental concept of embedded programming. In this article you will find how to do bitwise operations with details.

Basics of Bitwise Operation:

Bitwise operation means doing any kind of manipulation on the data on bit level by using the well known bitwise operators.

Here the data is the binary value loaded on a register (in microprocessor level) or on a variable (in programming language perspective). The data can be an integer, character, floating number or any other data type, but from the processor perspective it is simply a binary value.

To recap further, the below image is basic building block for the bitwise operations.

Bits bytes diagram

Basics of Bitwise Operators:

The below list of operations can be performed on bit level

  • Bitwise AND
  • Bitwise OR
  • Bitwise XOR
  • Bitwise NOT
  • Bitwise NOR
  • Bitwise NAND
  • Set a particular bit
  • Clear a particular bit
  • Toggle a particular bit
  • Left shift the bits
  • Right shift the bits

Bitwise AND operation:

AND is represented by the symbol ampersand (&)

If both bits are TRUE, then the result is TRUE, or 1. Otherwise the result is FALSE, or 0.

The Truth table of bitwise AND operation is below:

x y x AND y (or) x & y
0 0 0
0 1 0
1 0 0
1 1 1

Example bitwise AND operation:

x = 55 = 00110111

y = 75 = 01001011

x & y = 00000011 = 3

Bitwise OR operation:

OR is represented by the symbol pipe (|)

If either one of the bit is TRUE, then the result is TRUE, or 1. Otherwise the result is FALSE, or 0.

The Truth table of bitwise OR operation is below:

x y x OR y (or) x | y
0 0 0
0 1 1
1 0 1
1 1 1

Example bitwise OR operation:

x = 55 = 00110111

y = 75 = 01001011

x | y = 01111111 = 127

Bitwise XOR operation:

XOR is represented by the symbol upwards caret (^)

If the either one of the input is TRUE, then the result is TRUE, or 1.

If both inputs are TRUE/FALSE then the result is FALSE, or 0.

The Truth table of bitwise XOR operation is below:

x y x XOR y (or) x ^ y
0 0 0
0 1 1
1 0 1
1 1 0

Example bitwise XOR operation:

x = 55 = 00110111

y = 75 = 01001011

x ^ y = 01111100 = 124

Bitwise NOT:

XOR is represented by the symbol ~

Bitwise NOT operation means, one's complement or the bitwise complement of a given number.

This operation will invert each bits, thus change the bit value to 1 if it 0, or to the value 0 if it is 1.

The Truth table of bitwise NOT operation is below:

x ~ x (or) complement of x
0 1
1 0

Example bitwise NOT operation:

x = 55 = 00110111

~x = 11001000 = 200

Bitwise NOR:

There is no separate operator symbol for NOR operation.

Bitwise NOR is equivalent to performing the OR (|) operation between the two numbers and complementing the bits(~).

The Truth table of bitwise NOR operation is below:

x y x NOR y
0 0 1
0 1 0
1 0 0
1 1 0

Example bitwise NOR operation:

x = 55 = 00110111

y = 75 = 01001011

~(x | y) = 10000011 = 131

Bitwise NAND:

There is no separate operator symbol for NAND operation.

Bitwise NAND is equivalent to performing the AND (&) operation between the two numbers and complementing the bits(~).

The Truth table of bitwise NAND operation is below:

x y x NAND y
0 0 1
0 1 1
1 0 1
1 1 0

Example bitwise NAND operation:

x = 55 = 00110111

y = 75 = 01001011

~(x & y) = 11111100 = 252

Bit Set:

Setting a bit can be performed by using Bitwise OR operator.

As per the truth table of the OR operation, if either one of the bit is TRUE or 1 then the resulting bit will be TRUE or 1.

Since we do not know what is the bit value on the particular bit which we are going to set, the OR operation helps.

Example bit Set:

x = 55 = 00110111

a = 8 = 00001000 (to set Bit 3)

x | a = 00111111 = 63

Bit Clear:

Clearing a bit can be performed by using Bitwise AND, NOT operators.

As per the truth table of the AND operation, if the operand bit is 1, then the resultant bit is same as the input bit. If the operand bit is 0, then the resultant bit is 0.

Hence we need to do AND operation on the input number against the Bitwise NOT of the operand number.

Example bit Clear:

x = 55 = 00110111

a = ~4 = 11111011 (to clear Bit 2)

x & ~a = 00110011 = 51

Bit Toggle:

Toggling a bit can be performed by using Bitwise XOR operator.

As per the truth table of the XOR operation, if the operand bit is 1, then the resultant bit 0 if the input bit is 1, or the resultant bit is 1 if the input bit is 0.

Example bit Toggle:

x = 55 = 00110111

a = 12 = 00001100 (to toggle Bit 3 and Bit 2)

x ^ a = 00111011 = 59

Bit Left Shift:

Left shifting of the bits can be performed using the operator <<

Example bit left shift:

x = 55 = 00110111

x << 2 = 11011100 = 220

Bit Right Shift:

Right shifting of the bits can be performed using the operator >>

Example bit right shift:

x = 55 = 00110111

x >> 2 = 00001101 = 13


See also: