C/C++  

 

 

 

Bit Manipulation

I like C language better than others in most case. However, one thing that I am not happy with C language is that it does not have much built-in functions for bit manipulation. In most application, you wouldn't need this kind of bit manipulation but if you are working with Embedded System programming or Parsing program, you would need to do a lot of bit manipulation. In this page, I will post tricks for manipulating bits and bytes in various situations. It is not difficult but would look confusing if you are not familiar with these tricks. I will keep adding examples of those bit manipulation in diverse situations so that you can use this as a small dictionary.

 

Example 1 > Printing Numbers in Binary Format

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    printInt2Bin(123);

    printf("\n");

    printChar2Bin(123);

 

    return 1;

}

 

Result :----------------------------------

    printInt2Bin(123);    ==> 00000000 00000000 00000000 01111011

    printChar2Bin(123);  ==> 01111011

 

Example 2 > Shifting the binary number

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

    

    printf("a (Dec)  => %d\n",a);

    printf("a (Bin)  => ");

    printChar2Bin(a);

    

    printf("\n");

    printf("(a >> 1) => ");

    printChar2Bin(a >> 1);  // Shift the number 'a' to the right by 1 bit

    

    printf("\n");

    printf("(a >> 4) => ");

    printChar2Bin(a >> 4);  // Shift the number 'a' to the right by 4 bits

    

    printf("\n");

    printf("(a >> 8) => ");

    printChar2Bin(a >> 8); // Shift the number 'a' to the right by 8 bits

    

    printf("\n");

    printf("(a >> 10)=> ");

    printChar2Bin(a >> 10); // Shift the number 'a' to the right by 10 bits

    

    printf("\n");

    printf("(a << 1) => ");

    printChar2Bin(a << 1); // Shift the number 'a' to the left by 4 bit

    

    printf("\n");

    printf("(a << 4) => ");

    printChar2Bin(a << 4); // Shift the number 'a' to the left by 4 bits

    

    printf("\n");

    printf("(a << 8) => ");

    printChar2Bin(a << 8); // Shift the number 'a' to the left by 8 bits

    

    printf("\n");

    printf("(a << 10)=> ");

    printChar2Bin(a << 10); // Shift the number 'a' to the left by 10 bits

    

    return 1;

}

 

Result :----------------------------------

    a (Dec)  => 100

    a (Bin)  => 01100100

    (a >> 1) => 00110010

    (a >> 4) => 00000110

    (a >> 8) => 00000000

    (a >> 10)=> 00000000

    (a << 1) => 11001000

    (a << 4) => 01000000

    (a << 8) => 00000000

    (a << 10)=> 00000000

 

Example 3 > Take out a specific bit

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

    

    printf("a (Dec)  => %d\n",a);

    printf("a (Bin)  => ");

    printChar2Bin(a);

    

    printf("\n");

    printf("(a >> 0) & 1 => %d", (a >> 0) & 1 );

    

    printf("\n");

    printf("(a >> 1) & 1 => %d", (a >> 1) & 1 );

    

    printf("\n");

    printf("(a >> 2) & 1 => %d", (a >> 2) & 1 );

    

    printf("\n");

    printf("(a >> 3) & 1 => %d", (a >> 3) & 1 );

    

    printf("\n");

    printf("(a >> 4) & 1 => %d", (a >> 4) & 1 );

    

    printf("\n");

    printf("(a >> 5) & 1 => %d", (a >> 5) & 1 );

    

    printf("\n");

    printf("(a >> 6) & 1 => %d", (a >> 6) & 1 );

    

    printf("\n");

    printf("(a >> 7) & 1 => %d", (a >> 7) & 1 );

    

    return 1;

}

 

Result :----------------------------------

    a (Dec)  => 100

    a (Bin)  => 01100100

    (a >> 0) & 1 => 0

    (a >> 1) & 1 => 0

    (a >> 2) & 1 => 1

    (a >> 3) & 1 => 0

    (a >> 4) & 1 => 0

    (a >> 5) & 1 => 1

    (a >> 6) & 1 => 1

    (a >> 7) & 1 => 0

 

Example 4 >  Setting a specific bit (set it to be '1') and Unsetting all other bits (set it to be '0')

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

    

    printf("a (Dec)        => %d\n",a);

    printf("a (Bin)        => ");

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 0) => ");

    a = (1 << 0);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 1) => ");

    a = (1 << 1);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 2) => ");

    a = (1 << 2);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 3) => ");

    a = (1 << 3);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 4) => ");

    a = (1 << 4);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 5) => ");

    a = (1 << 5);

    printChar2Bin(a);

 

    printf("\n");

    printf("a = (1 << 6) => ");

    a = (1 << 6);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 7) => ");

    a = (1 << 7);

    printChar2Bin(a);

    

 

    return 1;

}

 

Result :------------------------------

    a (Dec)        => 100

    a (Bin)        => 01100100 // this existing value does not affect any of the following operation

    a = (1 << 0) => 00000001

    a = (1 << 1) => 00000010

    a = (1 << 2) => 00000100

    a = (1 << 3) => 00001000

    a = (1 << 4) => 00010000

    a = (1 << 5) => 00100000

    a = (1 << 6) => 01000000

    a = (1 << 7) => 10000000

 

Example 5 > Unsetting a specific bit (set it to be '0') and Setting all other bits (set it to be '1')

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

    

    printf("a (Dec)        => %d\n",a);

    printf("a (Bin)        => ");

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 0) => ");

    a = ~(1 << 0);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 1) => ");

    a = ~(1 << 1);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 2) => ");

    a = ~(1 << 2);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 3) => ");

    a = ~(1 << 3);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 4) => ");

    a = ~(1 << 4);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 5) => ");

    a = ~(1 << 5);

    printChar2Bin(a);

 

    printf("\n");

    printf("a = (1 << 6) => ");

    a = ~(1 << 6);

    printChar2Bin(a);

    

    printf("\n");

    printf("a = (1 << 7) => ");

    a = ~(1 << 7);

    printChar2Bin(a);

    

 

    return 1;

}

 

Result :----------------------------

    a (Dec)        => 100

    a (Bin)        => 01100100 // this existing value does not affect any of the following operation

    a = (1 << 0) => 11111110

    a = (1 << 1) => 11111101

    a = (1 << 2) => 11111011

    a = (1 << 3) => 11110111

    a = (1 << 4) => 11101111

    a = (1 << 5) => 11011111

    a = (1 << 6) => 10111111

    a = (1 << 7) => 01111111

 

Example 6 > Setting a specific bit (set it to be '1') without affecting other bits

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

    

    printf("a (Dec)        => %d\n",a);

    printf("a (Bin)        => ");

    printChar2Bin(a);

    

    printf("\n");

    printf("a | (1 << 0) => ");

    printChar2Bin(a | (1 << 0));

    

    printf("\n");

    printf("a | (1 << 1) => ");

    printChar2Bin(a | (1 << 1));

    

    printf("\n");

    printf("a | (1 << 2) => ");

    printChar2Bin(a | (1 << 2));

    

    printf("\n");

    printf("a | (1 << 3) => ");

    printChar2Bin(a | (1 << 3));

    

    printf("\n");

    printf("a | (1 << 4) => ");

    printChar2Bin(a | (1 << 4));

    

    printf("\n");

    printf("a | (1 << 5) => ");

    printChar2Bin(a | (1 << 5));

 

    printf("\n");

    printf("a | (1 << 6) => ");

    printChar2Bin(a | (1 << 6));

    

    printf("\n");

    printf("a | (1 << 7) => ");

    printChar2Bin(a | (1 << 7));  

    

    return 1;

}

 

Result :----------------------------------

    a (Dec)        => 100

    a (Bin)        => 01100100

    a | (1 << 0) => 01100101

    a | (1 << 1) => 01100110

    a | (1 << 2) => 01100100

    a | (1 << 3) => 01101100

    a | (1 << 4) => 01110100

    a | (1 << 5) => 01100100

    a | (1 << 6) => 01100100

    a | (1 << 7) => 11100100

 

Example 7 > Unsetting a specific bit (set it to be '0') without affecting other bits

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

    

    printf("a (Dec)         => %d\n",a);

    printf("a (Bin)         => ");

    printChar2Bin(a);

    

    printf("\n");

    printf("a & ~(1 << 0) => ");

    printChar2Bin(a & ~(1 << 0));

    

    printf("\n");

    printf("a & ~(1 << 1) => ");

    printChar2Bin(a & ~(1 << 1));

    

    printf("\n");

    printf("a & ~(1 << 2) => ");

    printChar2Bin(a & ~(1 << 2));

    

    printf("\n");

    printf("a & ~(1 << 3) => ");

    printChar2Bin(a & ~(1 << 3));

    

    printf("\n");

    printf("a & ~(1 << 4) => ");

    printChar2Bin(a & ~(1 << 4));

    

    printf("\n");

    printf("a & ~(1 << 5) => ");

    printChar2Bin(a & ~(1 << 5));

 

    printf("\n");

    printf("a & ~(1 << 6) => ");

    printChar2Bin(a & ~(1 << 6));

    

    printf("\n");

    printf("a & ~(1 << 7) => ");

    printChar2Bin(a & ~(1 << 7)); 

    

    return 1;

}

 

Result :----------------------------------

    a (Dec)         => 100

    a (Bin)         => 01100100

    a & ~(1 << 0) => 01100100

    a & ~(1 << 1) => 01100100

    a & ~(1 << 2) => 01100000

    a & ~(1 << 3) => 01100100

    a & ~(1 << 4) => 01100100

    a & ~(1 << 5) => 01000100

    a & ~(1 << 6) => 00100100

    a & ~(1 << 7) => 01100100

 

Example 8 > Setting multiple bit (set it to be '1') and Unsetting all other bits (set it to be '0')

#include <stdio.h>

 

void printInt2Bin(int a)

{

    int i,n;

    

    n = sizeof(int)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

void printChar2Bin(int a)

{

    int i,n;

    

    n = sizeof(char)*8;

    

    for(i=n-1;i>=0;i--) {

      printf("%d",(a >> i) & 1);

      if(i % 8 == 0) printf(" ");

    }

    

}

 

int main()

{

    

    char a = 100;

 

    printf("a (Dec)                        => %d\n",a);

    printf("a (Bin)                        => ");

    printChar2Bin(a);

    

    printf("\n");

    a = (1 << 5) | (1 << 3) | (1 << 0);

    printf("(1 << 5) | (1 << 3) | (1 << 0) => ");

    printChar2Bin(a);

    

    return 1;

}

 

Result :-----------------

    a (Dec)                        => 100

    a (Bin)                        => 01100100

    (1 << 5) | (1 << 3) | (1 << 0) => 00101001  // Set the bits 0,3,5 to be '1'

                                                // and set all other bits to be '0'

                                                // regardless of the existing value