Devs.site

Bitwise operations in C/C++

They are helpful when you work at a less abstract, lower level. You will find them in some old APIs, when doing checksums to validate data, in encryption algorithms, drivers and graphics programming.

In this little tutorial we will see what each one of them does and how we can use them in C / C++


These operations handle one byte at a time, going bit by bit (8 times) and applying the specified change. They may be mistaken by logical operations, which are based on the same rules but take variables as operands (which have more than one byte), instead of going bit by bit inside a single byte.

For the following examples we will have two variables:

unsigned char a = 24;  // in binary this is 00011000
unsigned char b = 111; // in binary this is 01101111

Now, these six operators are:

AND operator
It will compare each bit of two operands and return 1 in the position where both a and b have 1. In any other case the resulted bit will be 0

In C/C++ we use the (&) operator for a bitwise AND operation:

unsigned char c = a & b;
printf("Bitwise AND: %d \n", c);

Bitwise AND: 8

00011000 &
01101111 =
00001000

You can see how in the final result there's a 1 only where both a and b have 1

OR operator
Will compare each bit of the two operands and return 0 where both a and b have 0. In any other case the resulted bit will be 1

In C/C++ we use the (|) operator for a bitwise OR operation:

    unsigned char d = a | b;
    printf("Bitwise OR: %d \n", d);

Bitwise OR: 127

    00011000 |
    01101111 =
    01111111

In this case only the first bits are 0 in both operands so the result has a 0 in the first bit and 1 in the rest of them

XOR operator (Exclusive OR)
Will return 1 only when at least one of the bits at the same position will be 1. In any other case (both bits have 0 or both bits have 1) it will return 0

In C/C++ we use the (^) operator for a bitwise XOR operation:

    unsigned char e = a ^ b;
    printf("Bitwise XOR: %e \n", e);

Bitwise XOR: 119

    00011000 |
    01101111 =
    01110111

Wherever we have 1 and 1 or 0 and 0 at the same position of both operands, the result is 0

NOT operator
This basic operator works on a single value and simply inverts each bit

In C/C++ we use the (~) operator in front of the variable for a bitwise NOT operation:

    unsigned char f = ~a;
    printf("Bitwise NOT: %e \n", f);

Bitwise NOT: 231

    ~00011000 =
      11100111

It is obvious what happened here. Each 0 becomes 1, each 1 becomes 0 in the result

SHIFT operators
With these ones you can shift bits in a byte to the left or to the right. You can specify by how many places by using a number. Each bit will move to the left or to the right by how many places you specify. Bits that exceed the total of 8 that a byte can hold will normally be lost and empty spots will usually be zeros. This depends on each compiler.

In C/C++ we use the (<<) operator followed by the number that specifies the number of places to execute a left shift:

    unsigned char g = b<<1;
    unsigned char h = b>>1;
    printf("Bitwise LEFT SHIFT: %e \n", g);
    printf("Bitwise RIGHT SHIFT: %e \n", h);

Bitwise LEFT SHIFT: 222

    01101111 << 1 =
    11011110

Bitwise RIGHT SHIFT: 55

    01101111 >> 1 =
    00110111

You can see how each bit from b has moved one place to the left or to the right and the new one that came in from the right or from the left has 0


Code

    #include <iostream>
    int main()
    {
        unsigned char a = 24;
        unsigned char b = 111;

        unsigned char c = a & b;
        unsigned char d = a | b;
        unsigned char e = a ^ b;
        unsigned char f = ~a;
        unsigned char g = b << 1;
        unsigned char h = b>>1;

        printf("Bitwise AND: %d \n", c);
    printf("Bitwise OR: %d \n", d);
        printf("Bitwise XOR: %d \n", e);
        printf("Bitwise NOT: %d \n", f);
        printf("Bitwise LEFT SHIFT: %d \n", g);
        printf("Bitwise RIGHT SHIFT: %d \n", h);
    }

Requirements

Any C/C++ compiler (even online compilers)


Instructions

Just copy and paste the code, play with it and see different results

0 comments

Specify your e-mail if you want to receive notifications about new comments and replies