Introduction to Bitwise Operators

Bitwise operators operate on the bits in integer values. There are six bitwise operators in all:

& Bitwise AND operator

| Bitwise OR operator

^ Bitwise EOR (Exclusive Or) operator

~ Bitwise NOT operator

>> Bitwise shift right operator

<< Bitwise shift left operator

The ~ operator is a unary operator; all of the other bitwise operators are binary operators. The bit-not ~ operator is thus perhaps the simplest example. The ~ operator flips the bits of its operand, changing the 1 bits to 0 and the 0 bits to 1.

#include <stdio.h>

int main(void){
    
    unsigned int i = 0;

    printf("%u\n", ~i);
    printf("%u\n", i);
    
    return 0;
    
}

The shift operators shift the bits in the left operand by the number of positions specified by the right operand. The left shift operator, <<, moves the bits to the left. Arithmetically, the left-shift operator is equivalent to multiplication by a power of two.

In the following program, we will shift a single 1 bit to the left to produce powers of two.

#include <stdio.h>

int main(void){
    
    unsigned int i = 1;
    int j = 0;
    
    for(j; j<8; j++){
        printf("%d\t", 1 << j);
    }

    putchar('\n');

    return 0;
    
}

The right shift operator moves bits to the right. This is an operation best done on unsigned integers.

#include <stdio.h>

int main(void){
    
    unsigned char i = 16;
    
    printf("%d\n", 16);

    i = i >> 2;

    printf("%d\n", 16 >> 2);

    i = i >> 2;

    printf("%d\n", 16 >> 4);

    return 0;

}

We can also use the binary bitwise operators in assignments, excluding, of course, the unary operator ~.

#include <stdio.h>

int main(void){

    int i = 1;
    int j = 1;
    i <<= 3;

    printf("i = %d\n", i);

    i >>= 2;

    printf("i = %d\n", i);

    return 0;

}

The bitwise AND operator, &, examines the bits of its two operands in parallel, and places a one in the position where both bits are 1, and a zero in all other positions. The bitwise OR operator, |, places a one in any position where either operand has a one.

#include <stdio.h>

int main(void){

    int i = 1;
    int j = 1;
    int k = 1;

    printf("1 << 7 = %d \t 1 << 6 = %d\n", 1 << 7, 1 << 6);
    printf("3 << 6 = %d \n", 3 << 6);
    printf("(1 << 7) + (1 << 6) = %d\n",
            (1 << 7) + (1 << 6));

    i <<= 7;
    j <<= 6;

    k = i | j;

    printf("i = %d \t j = %d\n", i, j);
    printf("k = i | j\n");
    printf("k = %d\n", k);

    k &= j;

    printf("k &= j\n");
    printf("k = %d\n", k);

    return 0;

}

Bitwise operators might seem a bit academic, but they’re used often enough. One use is to store multiple values in a single variable, which is how Linux file permissions are stored.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s