WHAT IS BITWISE OPERATOR IN C?

Dev Frank
3 min readDec 16, 2023

--

Can you guess what role ‘bits’ play in the world of computers, and how they might be manipulated for specific tasks?

Imagine you have a bunch of toys, each with different colors. Now, let’s say you have a special pair of glasses that can change the color of the toys in a cool way. These glasses have two buttons — one button is for turning things on, and the other is for turning things off.

Now, think of the toys as numbers, but instead of normal numbers, they’re in a secret code made up of 0s and 1s. Each 0 or 1 is like a switch on or off.

Here come the bitwise operators! They are like the buttons on your special glasses. There’s the AND button, the OR button, the XOR button, and the NOT button.

Bitwise operators are used to perform operations at bit level. Bit is the smallest storage unit in a computer’s memory.

  • 8 Bits = 1 Byte
  • 1024 kilobytes = 1 megabyte (MB)
  • 1024 megabytes = 1 gigabyte (GB)
  • 1024 gigabytes = 1 terabyte (TB)
  • And so on.

With these operators we can perform bit level expression, we can also manipulate data at bit level. We cannot store this expression 5 + 4 = 7 directly in a computer’s memory. The computer will have to convert them into binary form either in 4bits or 8bits but generally 8bits.

Bitwise operations can be performed on integer values and character values, but not on float and double values.

TYPES OF BITWISE OPERATORS

These six (6) operators are fundamental in bitwise manipulation and are commonly used in programming to perform operations at the binary level.

  • AND (&)
  • OR (|)
  • XOR (^)
  • NOT (~)
  • Left Shift (<<)
  • Right Shift (>>)

AND (&)

  • It is a binary operator
  • It takes two bits at a time and perform AND operation
  • AND (&) requires two bits before the operation can be performed
  • The results of AND is 1 when both bits are 1

EXPRESSION
10 — → 1010
&
12 — -> 1100
=
8 — -> 1000
10 & 12 = 8

OR (|)

  • It is a binary operator
  • It takes two bits at a time and perform OR operation
  • OR (|) requires two bits before the operation can be performed
  • Result of OR is 1, if both bits or one of the bit is 1
  • Result of OR is 0 when both bits are 0

EXPRESSION
7 — -> 0111
|
4 — -> 0100
=
7 — -> 0111
7 | 4 = 7

XOR (^)

  • It is a binary operator
  • XOR (^) requires two bits before the operation can be performed
  • Result of XOR 1, if both bits are different
  • Result of XOR 0, if both bits are the same

EXPRESSION
9 — -> 1001
^
12 — -> 1100
=
5 — -> 0101
9 ^ 12 = 5

NOT (~)

  • It is a unary operator
  • It’s job is to complement each bit one by one
  • Result of NOT is 0 when bit is 1
  • Result of NOT is 1 when bit is 0

EXPRESSION
~13 — -> 1101
=
2 — -> 0010
~13 = 2

Left Shift (<<)

  • Shifts the bits of a binary number to the left by a specified number of positions
  • Then it fills the vacant positions on the right are with zeros

EXPRESSION
10 — -> 1010 << 1
=
4 — -> 0100
Here we had 1010 then it was left shifted once, making the 1 (first digit by the left) to be truncated thereby replacing the vacant space by the right with 0. We then have 0100.

Right Shift (>>)

  • Shifts the bits of a binary number to the right by a specified number of positions
  • Then it fills the vacant positions on the left are with zeros

EXPRESSION
7 — -> 0111 >> 1
=
3 — -> 0011
Same thing here, we had 0011 then it was right shifted once, making the 1(first digit by the left) to be truncated thereby replacing the vacant space by the left with 0. We then have 0110.

Bitwise operations is one of the easiest topics I have come across as a student in my software engineering program
I know with this explanations I have made on bitwise operators, writing a program on bitwise operations/ manipulation won’t be giving any hassles.

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.