× back Bitwise AND ( & ) Bitwise OR ( | ) Bitwise XOR ( ^ ) One's complement ( ~ ) Bitwise left shift ( << ) Bitwise right shift ( >> ) Programs Bit masking Application of bitwise operator
Next Topic → ← Previous Topic

Bitwise Manipulations

Bitwise AND ( & )

                
Bits of operand1    Bit of operand2     Resulting Bit
       0                 0                   0
       0                 1                   0
       1                 0                   0
       1                 1                   1
                
            

Program code ↓

                
#include<stdio.h>
int main()
{
    int a, b;
    printf("Enter values of a and b : ");
    scanf("%d%d", &a, &b);
    printf("a = %d\t\t", a);
    printf("b = %d\t\t", b);
    printf("a&b = %d\t\t", a&b);
    return 0;
}
/*
    if a = 8 and b = 9 then the output will be 8
      1000
    & 1001
    -------
      1000
*/
                
            

Bitwise OR ( | )

                
Bits of operand1    Bit of operand2     Resulting Bit
       0                 0                   0
       0                 1                   1
       1                 0                   1
       1                 1                   1
                
            

Program code ↓

                
#include<stdio.h>
int main()
{
    int a, b;
    printf("Enter values of a and b : ");
    scanf("%d%d", &a, &b);
    printf("a = %d\t\t", a);
    printf("b = %d\t\t", b);
    printf("a&b = %d\t\t", a|b);
    return 0;
}
/*
    if a = 8 and b = 9 then the output will be 9
      1000
    | 1001
    -------
      1001
*/
                
            

Bitwise XOR ( ^ )

                
Bits of operand1    Bit of operand2     Resulting Bit
       0                 0                   0
       0                 1                   1
       1                 0                   1
       1                 1                   0
                
            

Program code ↓

                
#include<stdio.h>
int main()
{
    int a, b;
    printf("Enter values of a and b : ");
    scanf("%d%d", &a, &b);
    printf("a = %d\t\t", a);
    printf("b = %d\t\t", b);
    printf("a&b = %d\t\t", a^b);
    return 0;
}
/*
    if a = 8 and b = 9 then the output will be 1
      1000
    ^ 1001
    -------
      0001
*/
                
            

One's Complement ( ~ )

                
Bits of operand        Resulting Bit
       0                     1
       1                     0

       
a   0010  1001  0011  1011    (0x293B)
~a  1101  0110  1100  0100    (0xD6C4)

b   0001  1010  0010  1111    (0x1A2F)
~b  1110  0101  1101  0000    (0xE5D0)
                
            

Program code ↓

                
#include<stdio.h>
int main()
{
    unsigned int a;
    printf("Enter values of a : ");
    scanf("%d", &a);
    printf("a = %d\n", a);
    printf("Signed result of ~a = %d\n", ~a);
    printf("Unsigned result of ~a = %u\n", ~a);
    return 0;
}
/*
    if a = 1 then the output will be -2 (signed) and 4294967294 (unsigned)
*/
                
            

Bitwise Left Shift ( << )

                
#include <stdio.h>
int main()
{
    int a;
    printf("Enter values of a : ");
    scanf("%d", &a);
    a = a << 2;
    printf("a << 2 = %d\t\t", a);
    return 0;
}
                
            

Output ↓

                        
Enter values of a : 1
a << 2 = 4 
                        
                    

Bitwise Right Shift ( >> )

                
#include <stdio.h>
int main()
{
    int a;
    printf("Enter values of a : ");
    scanf("%d", &a);
    a = a >> 2;
    printf("a >> 2 = %d\t\t", a);
    return 0;
}
                
            

Output ↓

                
Enter values of a : 9
a << 2 = 2  
                
            

Some programs

Program to swap two number using bitwise operator

                    
#include <stdio.h>
int main()
{
    int a = 10, b = 7;
    printf("The value of a and b before swap : a = %d & b = %d\n", a, b);
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("The value of a and b after swap : a = %d & b = %d\n", a, b);
    return 0;
}
                    
                

Program to enter a decimal number and print its binary form and check whether it is even or odd.

                       
#include <stdio.h>

void decimalToBinary(int num)
{
    int binary[32];
    int i = 0;

    // Convert decimal to binary using bit manipulation
    while (num > 0)
    {
        binary[i] = num & 1;
        num >>= 1;
        i++;
    }

    printf("Binary representation: ");

    // Print binary representation in reverse order
    for (int j = i - 1; j >= 0; j--)
    {
        printf("%d", binary[j]);
    }

    printf("\n");
}

void checkEvenOdd(int num)
{
    // Check the least significant bit to determine even/odd
    if ((num & 1) == 0)
    {
        printf("The number is even.\n");
    }
    else
    {
        printf("The number is odd.\n");
    }
}

int main()
{
    int decimalNum;

    printf("Enter a decimal number: ");
    scanf("%d", &decimalNum);

    decimalToBinary(decimalNum);
    checkEvenOdd(decimalNum);

    return 0;
}
                       
                   

Bit masking

checking if a bit is set (1) or not

                       
#include <stdio.h>
int isBitSet(int num, int bitPos)
{
    int mask = 1 << bitPos;
    return ((num & mask) > 0);
}
int main()
{
    int num = 10; // Binary: 0000 1010

    // Check if bit at position 3 is set
    if (isBitSet(num, 3))
        printf("Bit at position 3 is set.\n");
    else
        printf("Bit at position 3 is not set.\n");

    return 0;
}
                       
                   
  • Explanation: In this function, the bit at the desired position is isolated using the left shift operator (<<) to shift 1 by bitPos positions. This creates a bit mask with only the bit at the desired position set to 1. The bitwise AND operation (&) is then performed between num and the bit mask. If the result is non-zero, it means the bit at the specified position is set (1); otherwise, it is not set (0). The function returns this result.
  • Note: Bit numbering starts from 0 so position bit 3 means 4th location from rightmost bit.

Setting a specific bit

  • We are going to use 'setBit()' function.
  • The setBit() function is used to set a specific bit to 1 in a given number. It takes two parameters: num, which represents the number you want to modify, and bitPos, which indicates the position of the bit you want to set. Here's the function implementation:
                       
#include <stdio.h>
// Function to set a specific bit
int setBit(int num, int bitPos)
{
    int mask = 1 << bitPos;
    return num | mask;
}
int main()
{
    int num = 10; // Binary: 0000 1010

    // Set bit at position 2
    printf("Initially number was : %d\n", num);
    num = setBit(num, 2);
    printf("After setting bit at position 2 : %d\n", num);

    return 0;
}
                       
                   

Output ↓

                    
Initially number was : 10
After setting bit at position 2 : 14
                    
                
  • num in binary was 0000 1010 = 10
    after shifting 1 (0000 0001) by 2 we get 4 (0000 0100)
    0000 1010 | 0000 0100 = 0000 1110 = 14

Clearing a bit

  • The clearBit() function is used to clear a specific bit to 0 in a given number. It takes two parameters: num, which represents the number you want to modify, and bitPos, which indicates the position of the bit you want to clear. Here's the function implementation:
                       
#include <stdio.h>
// Function to clear a specific bit
int clearBit(int num, int bitPos)
{
    int mask = 1 << bitPos;
    return num & ~mask;
}
int main()
{
    int num = 10; // Binary: 0000 1010

    // Clear bit at position 1
    num = clearBit(num, 1);
    printf("After clearing bit at position 1: %d\n", num);
    
    return 0;
}
                       
                   

Output ↓

                    
After clearing bit at position 1: 8
                    
                
  • In this example, we clear the bit at position 1 (counting from the rightmost bit starting at 0) to 0 in the number num, which is initially 10. The resulting value, after clearing the bit, is 8. This change occurs while preserving the other bits in the number.

Toggling a bit

  • The toggleBit() function is used to toggle a specific bit in a given number, which means changing it from 0 to 1 or from 1 to 0. It takes two parameters: num, which represents the number you want to modify, and bitPos, which indicates the position of the bit you want to toggle. Here's the function implementation:
                       
#include <stdio.h>
// Function to toggle a specific bit
int toggleBit(int num, int bitPos)
{
    int mask = 1 << bitPos;
    return num ^ mask;
}
int main()
{
    int num = 10; // Binary: 0000 1010

    // Toggle bit at position 0
    num = toggleBit(num, 0);
    printf("After toggling bit at position 0: %d\n", num);
    
    return 0;
}
                       
                   

Output ↓

                    
After toggling bit at position 0: 11
                    
                
  • In this example, we toggle the bit at position 0 (counting from the rightmost bit starting at 0) in the number num, which is initially 10. The resulting value, after toggling the bit, is 11. This change occurs while preserving the other bits in the number.

Application of bitwise operator