Advertisement

Programmer Coding

Bitwise Operator in C Language

What is Bitwise Operator in C?

The bitwise operators are the operators used to carry out the operations at the records at the bit-level. while we perform the bitwise operations, then it’s also referred to as bit-level programming. It consists of  digits, both zero or 1. it’s miles particularly utilized in numerical computations to make the calculations quicker.

we have one of a kind kinds of bitwise operators inside the c programming language. the subsequent is the list of the bitwise operators:

Advertisement

Let’s look at the truth table of the bitwise operators.

  • Bitwise AND (&):

Performs bitwise AND operation in the combination of two operations. If both products are 1, the result is 1, otherwise 0.

Example
  1. #include <stdio.h>
  2. int main() {
  3. int a = 5; // Binary: 00000101
  4. int b = 3; // Binary: 00000011
  5. int result = a & b; // Result: 00000001 (1)
  6. printf(“Bitwise AND: %d\n”, result);
  7. return 0;
  8. }

Output

Bitwise AND: 1

  • Bitwise OR (|):

Performs a bit OR operation on the combination of two operators. If at least one bit is 1, the result is 1, otherwise 0.

Example
  1. #include <stdio.h>
  2. int main() {
  3. int a = 5; // Binary: 00000101
  4. int b = 3; // Binary: 00000011
  5. int result = a | b; // Result: 00000111 (7)
  6. printf(“Bitwise OR: %d\n”, result);
  7. return 0;
  8. }

Output

Bitwise OR: 7

  • Bitwise XOR (^):

Performs a special bitwise OR (XOR) operation on the sum of two operators. The result is 1 if the objects are different, 0 otherwise.

Example
  1. #include <stdio.h>
  2. int main() {
  3. int a = 5; // Binary: 00000101
  4. int b = 3; // Binary: 00000011
  5. int result = a ^ b; // Result: 00000110 (6)
  6. printf(“Bitwise XOR: %d\n”, result);
  7. return 0;
  8. }

Output

Bitwise XOR: 6

  • Bitwise Not (~):

Perform NOT bit (completion) of all bit operations. Flips each bit, changing 1 to 0 and 0 to 1.

Example
  1. #include <stdio.h>
  2. int main() {
  3. int a = 5; // Binary: 00000101
  4. int result = ~a; // Result: 11111010 (-6)
  5. printf(“Bitwise NOT: %d\n”, result);
  6. return 0;
  7. }

Output

Bitwise NOT: -6

  • Left shift (<<):

Shifts all bits in the left operand to the left by the operation number. The empty position on the right is filled with zero.

Example
  1. #include <stdio.h>
  2. int main() {
  3. int a = 5; // Binary: 00000101
  4. int result = a << 2; // Result: 00010100 (20)
  5. printf(“Left Shift: %d\n”, result);
  6. return 0;
  7. }

Output

Left Shift: 20

  • Right shift (>>):

Shifts all bits in the left operand to the right by the operation number. The empty bits on the left are written as signed bits (for signed numbers) or zeros (for unsigned numbers).

Example
  1. #include <stdio.h>
  2. int main() {
  3. int a = 5; // Binary: 00000101
  4. int result = a >> 2; // Result: 00000001 (1)
  5. printf(“Right Shift: %d\n”, result);
  6. return 0;
  7. }

Output

Right Shift: 1

Examples

#include <stdio.h>

int main() {

int x = 5;    // Binary representation: 00000101

int y = 3;    // Binary representation: 00000011

Advertisement

// Bitwise AND

printf(“Bitwise AND: %d\n”, x & y);  // Output: 00000001 (1)

// Bitwise OR

printf(“Bitwise OR: %d\n”, x | y);   // Output: 00000111 (7)

// Bitwise XOR

printf(“Bitwise XOR: %d\n”, x ^ y);  // Output: 00000110 (6)

// Bitwise NOT

printf(“Bitwise NOT of x: %d\n”, ~x);  // Output: 11111010 (-6)

// Left Shift

printf(“Left Shift of x by 2 positions: %d\n”, x << 2);  // Output: 00010100 (20)

// Right Shift

printf(“Right Shift of x by 2 positions: %d\n”, x >> 2); // Output: 00000001 (1)

return 0;

}

Output

Bitwise AND: 1

Bitwise OR: 7

Bitwise XOR: 6

Bitwise NOT of x: -6

Left Shift of x by 2 positions: 20

Right Shift of x by 2 positions: 1

Explain

 

  • Bitwise AND of 5 and 3 results in 1 (00000101 & 00000011 = 00000001).
  • Bitwise OR of 5 and 3 results in 7 (00000101 | 00000011 = 00000111).
  • Bitwise XOR of 5 and 3 results in 6 (00000101 ^ 00000011 = 00000110).
  • Bitwise NOT of 5 results in -6 ( ~00000101 = 11111010).
  • Left shifting 5 by 2 positions results in 20 (00000101 << 2 = 00010100).
  • Right shifting 5 by 2 positions results in 1 (00000101 >> 2 = 00000001)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top