Skip to main content

Bitwise Operators in C Programming

 

Bitwise AND operator &


In this tutorial you will learn about all 6 bitwise operators in C programming with examples.

The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.

Let us suppose the bitwise AND operation of two integers 12 and 25.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
  00001100
& 00011001
  ________
  00001000  = 8 (In decimal)

Example #1: Bitwise AND

#include <stdio.h>
int main()
{
    int a = 12, b = 25;
    printf("Output = %d", a&b);
    return 0;
}

Output

Output = 8

Bitwise OR operator |

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted by |.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25
  00001100
| 00011001
  ________
  00011101  = 29 (In decimal)

Example #2: Bitwise OR

#include <stdio.h>
int main()
{
    int a = 12, b = 25;
    printf("Output = %d", a|b);
    return 0;
}

Output

Output = 29

Bitwise XOR (exclusive OR) operator ^

The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25
  00001100
^ 00011001
  ________
  00010101  = 21 (In decimal)

Example #3: Bitwise XOR

#include <stdio.h>
int main()
{
    int a = 12, b = 25;
    printf("Output = %d", a^b);
    return 0;
}

Output

Output = 21

Bitwise complement operator ~

Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~.

35 = 00100011 (In Binary)

Bitwise complement Operation of 35
~ 00100011 
  ________
  11011100  = 220 (In decimal)

Twist in bitwise complement operator in C Programming

The bitwise complement of 35 (~35) is -36 instead of 220, but why?

For any integer n, bitwise complement of n will be -(n+1). To understand this, you should have the knowledge of 2's complement.

2's Complement

Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:

 Decimal         Binary           2's complement 
   0            00000000           -(11111111+1) = -00000000 = -0(decimal)
   1            00000001           -(11111110+1) = -11111111 = -256(decimal)
   12           00001100           -(11110011+1) = -11110100 = -244(decimal)
   220          11011100           -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.

The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.

Bitwise complement of any number N is -(N+1). Here's how:

bitwise complement of N = ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)

Example #4: Bitwise complement

#include <stdio.h>
int main()
{
    printf("Output = %d\n",~35);
    printf("Output = %d\n",~-12);
    return 0;
}

Output

Output = -36
Output = 11

Shift Operators in C programming

There are two shift operators in C programming:

  • Right shift operator
  • Left shift operator.

Right Shift Operator

Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.

212 = 11010100 (In binary)
212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000 
212>>0 = 11010100 (No Shift)

Left Shift Operator

Left shift operator shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0. The symbol of the left shift operator is <<.

212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

Example #5: Shift Operators

#include <stdio.h>
int main()
{
    int num=212, i;
    for (i=0; i<=2; ++i)
        printf("Right shift by %d: %d\n", i, num>>i);

     printf("\n");

     for (i=0; i<=2; ++i) 
        printf("Left shift by %d: %d\n", i, num<<i);    
    
     return 0;
}

Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53

Left Shift by 0: 212
Left Shift by 1: 424
Left Shift by 2: 848
                 Thank You :)

Comments

Popular posts from this blog

C program to find prime factors of a number

  Write a C program to input a number from user and find Prime factors of the given number using loop. C program to list all prime factors of a given number. Logic to find prime factors of a number in C programming. Example Input Input any number: 10 Output Prime factors of 10: 2, 5 Required knowledge Basic C programming ,  If statement ,  For loop ,  Nested loop What is Prime factor? Factors of a number  that are  prime numbers  are called as Prime factors of that number. For example: 2 and 5 are the prime factors of 10. Logic to check prime factors of a number Step by step descriptive logic to find prime factors. Input a number from user. Store it in some variable say  num . Run a loop from  2  to  num/2 , increment 1 in each iteration. The loop structure should look like  for(i=2; i<=num/2; i++) . You may think why loop from 2 to  num/2 ? Because prime number starts from 2 and any factor of a number  n  is ...

To Find Factorial Of A Number Using C Program

  Program 1: Factorial program in c using for loop #include <stdio.h> int main (){ int i , f = 1 , num ;   printf ( "Enter a number: " ); scanf ( "%d" ,& num );   for ( i = 1 ; i <= num ; i ++) f = f * i ;   printf ( "Factorial of %d is: %d" , num , f ); return 0 ; } Result Enter a number: 8 Factorial of 8 is: 40320 Program 2: Factorial program in c using pointers #include <stdio.h>   void findFactorial ( int , int *); int main (){ int i , factorial , num ;   printf ( "Enter a number: " ); scanf ( "%d" ,& num );   findFactorial ( num ,& factorial ); printf ( "Factorial of %d is: %d" , num ,* factorial );   return 0 ; }   void findFactorial ( int num , int * factorial ){ int i ;   * factorial = 1 ;   for ( i = 1 ; i <= num ; i ++) * factorial =* factorial * i ; } Result Enter a number: 8 Factorial of 8 is: 40320 Program 3: Factorial progra...

Questions of C Program

1. Write a C program to find the area of a circle. 2. Write a C program to find the area of a triangle. 3. Write a C program to swap two numbers without temp variable (or a third variable) 4. Write a C program to print ASCII value of the character 5. Write a C program to read a character in uppercase and then print in lowercase. 6. Write a C program to swap two numbers with temp variable 7. Write a C program to calculate the average of two number 8. Write a C program to calculate the simple interest. 9. Write a C program to find the quotient and remainder of two numbers 10.Write a C program that displays the size of every datatype. 1. Write a c program to add two integers and print the result ,inputs must be taken using scanf function. 2. Write a c program to add two float numbers without third variable and print the result ,inputs must be taken using scanf function. 3. Write a c program, to subtract two integers and print the result, inputs must be taken using scanf function. 4. Write...