Menu Close

C Program : Bitwise Operators

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

1. What Are Bitwise Operators ?

Operators in C Program
Operators in C Program

Bitwise operators are used in C programming to perform bit-level operations.

Bitwise operators are not applicable to operands of type float, double, long double, or void. Bitwise operators are specifically designed to operate on integral data types, such as int, char, and long, by manipulating the individual bits of the operands.

Attempting to use bitwise operators on float, double, long double, or void operands will result in a compilation error or undefined behavior. These data types do not represent the data at the bit level, so bitwise operations are not meaningful or well-defined for them.

If you need to perform bitwise operations, make sure to use appropriate integer data types instead of floating-point or void types.

& Bitwise AND operator. Performs a logical AND operation on each corresponding bit of the operands. Returns 1 if both bits are 1, otherwise returns 0.

| Bitwise OR operator. Performs a logical OR operation on each corresponding bit of the operands. Returns 1 if at least one of the bits is 1, otherwise returns 0.

^ Bitwise XOR (exclusive OR) operator. Performs a logical XOR operation on each corresponding bit of the operands. Returns 1 if the bits are different, otherwise returns 0.

~ Bitwise NOT operator. Flips each bit of the operand, changing 0 to 1 and 1 to 0. Returns the complement of the operand.

<< Left shift operator. Shifts the bits of the left operand to the left by a specified number of positions. The empty positions are filled with zeros.

>> Right shift operator. Shifts the bits of the left operand to the right by a specified number of positions. The empty positions are filled based on the sign of the left operand.

Explanation:

(1) Except for the bitwise NOT operator (~), all other bitwise operators are binary operators, which means they require operands on both sides.

(2) The operands must be of integer or character types. They cannot be floating-point numbers.

(3) Bitwise operators operate on the binary representation of the data, specifically on the two’s complement representation. The bitwise operations are performed on each corresponding bit of the operands, allowing you to manipulate individual bits.

These bitwise operators are commonly used in tasks such as manipulating individual bits, setting or clearing specific flags or bits, performing bitwise operations on bit masks, and optimizing memory usage. It’s important to remember that bitwise operators operate on the binary representation of the data, and the operands must be integer or character types.

2. Truth Table Showing the Results of Bitwise Operators

Truth Table of &, |,  ^ 

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

3. Left Shift Operator and Right Shift Operator

3.1 example

a = 00010000
b = 2
a << b = 01000000
a >> b = 00000100

In the case of a << b, the value 00010000 is shifted to the left by 2 bits, and additional zeros are added on the right. Therefore, the value becomes 01000000. For a >> b, the value is shifted 2 bits to the right. Two zeros are removed from the right, and two zeros are added on the left. Therefore, the value becomes 00000100.

It’s important to note that shifting does not work like rotation, which means that the shifted bits are not added to the other end. The value of the shifted bits is lost.

4.~ Bitwise NOT operator

Let’s understand the bitwise NOT operator through an example:

unsigned int a = 44;  // binary: 00101100
unsigned int result = ~a;  // binary: 11010011

In this example, we have the variable a with a value of 44, which is represented in binary as 00101100. Applying the bitwise NOT operator (~) to a flips each bit, changing each 1 to 0 and each 0 to 1. As a result, we obtain 11010011 in binary.

So, the bitwise NOT operator complements each bit of the operand, effectively changing all 1s to 0s and all 0s to 1s. It is also known as the one’s complement operator.

Example 4.1 Bitwise NOT operator and Left Shift Operator and Right Shift Operator

#include <stdio.h>

int main() {

   int a = 0001000, b = 2, result;

   printf("a=%d | b=%d\n",a,b);

   // <<
   result = a<<b;
   printf("a << b = %d \n",result);

   // >>
   result = a>>b;
   printf("a >> b = %d \n",result);

   return 0;

}

Results:

a=512 | b=2
a << b = 2048 
a >> b = 128

Example 4.2 Bitwise operators

// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
   // a = 5(00000101), b = 9(00001001)
   unsigned char a = 5, b = 9;

   // The result is 00000001
   printf("a = %d, b = %d\n", a, b);
   printf("a&b = %d\n", a & b);

   // The result is 00001101
   printf("a|b = %d\n", a | b);

   // The result is 00001100
   printf("a^b = %d\n", a ^ b);

   // The result is 11111010
   printf("~a = %d\n", a = ~a);

   // The result is 00010010
   printf("b<<1 = %d\n", b << 1);

   // The result is 00000100
   printf("b>>1 = %d\n", b >> 1);

   return 0;

}

Results:

a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18 
b>>1 = 4

5. ^ Bitwise XOR (exclusive OR) operator.

The bitwise XOR (exclusive OR) operator is denoted by the symbol ^ and is used to perform a bitwise operation on two operands.

From a technical standpoint, the bitwise XOR operator is the most useful operator. It is used in many problems.

A simple example is “Given an array of numbers where all elements appear even times except for one number that appears odd times, find the number that appears odd times.”

This problem can be effectively solved by performing XOR operation on all the numbers.

It compares the corresponding bits of the operands and produces a new value where each bit is set to 1 if the two corresponding bits are different (one bit is 1 and the other is 0), and set to 0 if the two corresponding bits are the same (both 0 or both 1).

Let’s illustrate the bitwise XOR operator with an example:

unsigned int a = 10;  // binary: 00001010
unsigned int b = 6;   // binary: 00000110
unsigned int result = a ^ b;  // binary: 00001100

In this example, we have two variables a and b with values 10 and 6, respectively. When we apply the bitwise XOR operator (^) to a and b, it compares the corresponding bits of a and b and produces a new value result where each bit is set to 1 if the two corresponding bits are different, and set to 0 if the two corresponding bits are the same.

Here’s the breakdown of the bitwise XOR operation:

a: 00001010
b: 00000110
----------------
result: 00001100

So, the resulting value of result is 00001100 in binary, which is equal to 12 in decimal. The bitwise XOR operator can be used for various purposes, such as flipping specific bits, swapping values, or performing encryption algorithms.

Example 5.1  Bitwise XOR Operator – Find the Number That Only Show up odd Times


#include <stdio.h>

// Function to return the only odd
// occurring element
int findOdd(int arr[], int n)
{
   int res = 0, i;
   for (i = 0; i < n; i++)
   res ^= arr[i];
   return res;
}

int main(void)
{
   int arr[] = { 12, 12, 14, 90, 14, 14, 14 };
   printf("sizeof arr: %d\n", sizeof(arr));
   printf("sizeof arr[0]:%d\n", sizeof(arr[0]));
   int n = sizeof(arr) / sizeof(arr[0]);
   printf("The odd occurring element is %d ",
   findOdd(arr, n));
   return 0;
}

Results:

sizeof arr: 28
sizeof arr[0]:4
The odd occurring element is 90

 

Related Posts

Leave a Reply