Bit Operation In C Programming With Example & Applications

bit Operation In C

Hello guys, welcome back to our blog. Here in this article, we will discuss the bit operation in C programming with examples, and applications of bit operation in the real-time world, and we will share code for all operations.

If you have any electrical, electronics, and computer science doubts, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics.

Also, read:

Bit Operation In C Programming

In the C programming language, bit operations are operations that are performed on the individual bits of a number. These operations can be used to manipulate the bits of a number in various ways, such as setting, clearing, flipping, or checking the value of individual bits.

Here are some common bit operations in C:

  • The bitwise AND operator (&) is used to perform a bitwise AND operation on two numbers. The result of the operation is a number in which the bits are set to 1 only if the corresponding bits in both numbers are 1. For example, the expression 5 & 3 evaluates to 1, since the binary representation of 5 is 101 and the binary representation of 3 is 011, and the result of the AND operation is 001.
  • The bitwise OR operator (|) is used to perform a bitwise OR operation on two numbers. The result of the operation is a number in which the bits are set to 1 if either of the corresponding bits in the two numbers is 1. For example, the expression 5 | 3 evaluates to 7, since the binary representation of 5 is 101 and the binary representation of 3 is 011, and the result of the OR operation is 111.
  • The bitwise XOR operator (^) is used to perform a bitwise exclusive OR operation on two numbers. The result of the operation is a number in which the bits are set to 1 if the corresponding bits in the two numbers are different. For example, the expression 5 ^ 3 evaluates to 6, since the binary representation of 5 is 101 and the binary representation of 3 is 011, and the result of the XOR operation is 110.
  • The bitwise NOT operator (~) is used to perform a bitwise NOT operation on a number. This operation flips the value of each bit in the number so that 1s become 0s and 0s become 1s. For example, the expression ~5 evaluates to -6, since the binary representation of 5 is 00000101 and the result of the NOT operation is 11111010.
  • Right shift operation: In the C programming language, the right shift operator (>>) is used to shift the bits of a number to the right. This operator is commonly used to divide a number by a power of 2 or to perform bitwise operations on numbers.
  • Left shift operation: In the C programming language, the left shift operator (<<) is used to shift the bits of a number to the left. This operator is commonly used to multiply a number by a power of 2, or to perform bitwise operations on numbers.

01. AND operator in C

In the C programming language, the AND operator (&) is a bitwise operator that is used to perform a bitwise AND operation on two numbers. This operator compares each bit in the first operand with the corresponding bit in the second operand, and if both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

Here is an example of using the AND operator in C:

#include <stdio.h>

int main() {
  int x = 5;   // binary representation:  00000101
  int y = 3;   // binary representation:  00000011
  int z = x & y;  // binary representation: 00000001
  
  printf("%d & %d = %d\n", x, y, z);  // prints "5 & 3 = 1"
  
  return 0;
}

In this example, the AND operator is used to perform a bitwise AND operation on the numbers 5 and 3. The binary representation of 5 is 00000101 and the binary representation of 3 is 00000011. The result of the AND operation is 00000001, which is the binary representation of the number 1.

02. OR operator in C

In the C programming language, the OR operator (|) is a bitwise operator that is used to perform a bitwise OR operation on two numbers. This operator compares each bit in the first operand with the corresponding bit in the second operand, and if either bit is 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

Here is an example of using the OR operator in C:

#include <stdio.h>

int main() {
  int x = 5;   // binary representation:  00000101
  int y = 3;   // binary representation:  00000011
  int z = x | y;  // binary representation: 00000111
  
  printf("%d | %d = %d\n", x, y, z);  // prints "5 | 3 = 7"
  
  return 0;
}

In this example, the OR operator is used to perform a bitwise OR operation on the numbers 5 and 3. The binary representation of 5 is 00000101 and the binary representation of 3 is 00000011. The result of the OR operation is 00000111, which is the binary representation of the number 7.

03. XOR operator in C

In the C programming language, the XOR operator (^) is a bitwise operator that is used to perform a bitwise exclusive OR operation on two numbers. This operator compares each bit in the first operand with the corresponding bit in the second operand, and if the bits are different, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

Here is an example of using the XOR operator in C:

#include <stdio.h>

int main() {
  int x = 5;   // binary representation:  00000101
  int y = 3;   // binary representation:  00000011
  int z = x ^ y;  // binary representation: 00000110
  
  printf("%d ^ %d = %d\n", x, y, z);  // prints "5 ^ 3 = 6"
  
  return 0;
}

In this example, the XOR operator is used to perform a bitwise exclusive OR operation on the numbers 5 and 3. The binary representation of 5 is 00000101 and the binary representation of 3 is 00000011. The result of the XOR operation is 00000110, which is the binary representation of the number 6.

04. NOT operator in C

In the C programming language, the NOT operator (~) is a bitwise operator that is used to perform a bitwise NOT operation on a number. This operation flips the value of each bit in the number so that 1s become 0s and 0s become 1s.

Here is an example of using the NOT operator in C:

#include <stdio.h>

int main() {
  int x = 5;   // binary representation:  00000101
  int y = ~x;  // binary representation:  11111010
  
  printf("~%d = %d\n", x, y);  // prints "~5 = -6"
  
  return 0;
}

In this example, the NOT operator is used to perform a bitwise NOT operation on the number 5. The binary representation of 5 is 00000101, and the result of the NOT operation is 11111010, which is the binary representation of the number -6.

05. Right shift operation in C

In the C programming language, the right shift operator (>>) is used to shift the bits of a number to the right. This operator is commonly used to divide a number by a power of 2 or to perform bitwise operations on numbers.

The right shift operator has the following syntax:

x >> y

Here, x is the number to shift and y is the number of bits to shift. The expression x >> y divides x by 2^y, and the result is the integer value of the quotient. For example, the expression 20 >> 2 evaluates to 5, since 20 / 2^2 = 5.

The right shift operator can also be used in combination with other bitwise operators, such as the bitwise AND operator (&), to perform bitwise operations on numbers. For example, the expression (x & y) >> n performs a bitwise AND operation on x and y, and then shifts the result right by n bits.

Here is an example of using the right shift operator in C:

#include <stdio.h>

int main() {
  int x = 20;   // binary representation:  00010100
  int y = 2;    // number of bits to shift
  int z = x >> y;  // binary representation:  00000101
  
  printf("%d >> %d = %d\n", x, y, z);  // prints "20 >> 2 = 5"
  
  return 0;
}

In this example, the expression x >> y divides the number 20 by 2^2, and the result is the integer value of the quotient, which is 5. The binary representation of 20 is 00010100, and when shifted right by 2 bits, the result is 00000101, which is the binary representation of the number 5.

06. Left shift operation in C

In the C programming language, the left shift operator (<<) is used to shift the bits of a number to the left. This operator is commonly used to multiply a number by a power of 2 or to perform bitwise operations on numbers.

The left shift operator has the following syntax:

x << y

Here, x is the number to shift and y is the number of bits to shift. The expression x << y multiplies x by 2^y, and the result is the integer value of the product. For example, the expression 5 << 2 evaluates to 20, since 5 * 2^2 = 20.

The left shift operator can also be used in combination with other bitwise operators, such as the bitwise OR operator (|), to perform bitwise operations on numbers. For example, the expression (x | y) << n performs a bitwise OR operation on x and y, and then shifts the result left by n bits.

Here is an example of using the left shift operator in C:

#include <stdio.h>

int main() {
  int x = 5;   // binary representation:  00000101
  int y = 2;    // number of bits to shift
  int z = x << y;  // binary representation:  00010100
  
  printf("%d << %d = %d\n", x, y, z);  // prints "5 << 2 = 20"
  
  return 0;
}

In this example, the expression x << y multiplies the number 5 by 2^2, and the result is the integer value of the product, which is 20. The binary representation of 5.

This was about “Bit Operation In C Programming“. I hope this article may help you all a lot. Thank you for reading.

Also, read:

About The Author

Share Now