Skip to main content

Section 32.1 Bitwise Operators

C gives us direct access to the individual bits of an integer through a family of bitwise operators:
Table 32.1. Bitwise Operators
Operator Meaning
~ bitwise NOT (negation): flips every bit
<< left shift: shifts all bits left, filling with 0s
>> right shift: shifts all bits right
& bitwise AND: 1 only where both operands have a 1
| bitwise OR: 1 where either operand has a 1
These are entirely different from the logical operators &&, ||, and !, which operate on whole truth values rather than individual bits— don’t mix them up!
Let’s see the bitwise operators in action. The helper function printBinary() below prints out the binary representation of an unsigned integer, one bit at a time, using the division/remainder approach you may have already seen:

Compile and run the code below in your own C environment.

#include <stdio.h>
//prototypes
void printBinary(unsigned number);
     
int main (void){
    unsigned number, number_neg, number_shift;
    
    printf("Please enter a positive integer: ");
    scanf("%d", &number);
    printf("The number you entered is: %d\n", number);
    printBinary(number);
    //bitwise negation: ~
    printf("Bitwise negation of number:\n");
    number_neg = ~number;
    printBinary(number_neg);
    //left shift: <<
    printf("Shifting to the left:\n");
    number_shift = number << 4;
    printBinary(number_shift);
    //right shift: >>
    printf("Shifting to the right\n");
    printBinary(number_neg >> 3);
    
    return 0;
}
void printBinary(unsigned number){
    int i;
    int array[sizeof(unsigned)*8];
    for(i=0; i<sizeof(unsigned)*8; i++){
        array[i] = number%2;
        number = number/2;
    }
    for(i=sizeof(unsigned)*8-1; i>=0; i--){
        printf("%d", array[i]);
        if(i%8 == 0){
            printf(" ");
        }
    }
    printf("\n");
}
Now let’s add the AND (&) and OR (|) operators to the mix, applied to two numbers at once:

Compile and run the code below in your own C environment.

#include <stdio.h>
//prototypes
void printBinary(unsigned number);
     
int main (void){
    unsigned number, number2, number_neg, number_shift, result;
    
    printf("Please enter two positive integer: ");
    scanf("%d%d", &number, &number2);
    printf("The numbers you entered are: %d and %d\n", number, number2);
    printBinary(number);
    printBinary(number2);
    //bitwise negation: ~
    printf("Bitwise negation of number:\n");
    number_neg = ~number;
    printBinary(number_neg);
    //left shift: <<
    printf("Shifting to the left:\n");
    number_shift = number << 4;
    printBinary(number_shift);
    //right shift: >>
    printf("Shifting to the right\n");
    printBinary(number_neg >> 3);

    //bitwise AND: &
    printf("number & number2\n");
    result = number & number2;
    printBinary(result);

    //bitwise OR: |
    printf("number | number 2\n");
    result = number | number2;
    printBinary(result);
    
    return 0;
}
void printBinary(unsigned number){
    int i;
    int array[sizeof(unsigned)*8];
    for(i=0; i<sizeof(unsigned)*8; i++){
        array[i] = number%2;
        number = number/2;
    }
    for(i=sizeof(unsigned)*8-1; i>=0; i--){
        printf("%d", array[i]);
        if(i%8 == 0){
            printf(" ");
        }
    }
    printf("\n");
}
Time to practice— this time, instead of using division and remainders, use the bitwise operators themselves to read and print each bit directly.

Activity 32.1.

Write a C function that prints the binary representation of an unsigned integer entered by the user. Instead of using division and remainders, you must use bitwise operators (specifically, right-shift and AND) to read and print the bits directly.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void){
	unsigned int num;

	printf("Please enter a positive integer: ");
	scanf("%d", &num);

	printf("Decimal numnber: %d \n", num);

	printf("binary number: ");
	printBinary(num);
	printf("\n");

	return 0;
}

//Write your function here

Activity 32.2.

Write a C function that checks whether the \(k\)-th bit of an integer \(n\) is set (1) or not (0). The convention is to count bits from the right, starting at 0. In main(), ask the user to enter an integer as well as the bit position they want to check, call your function to evaluate the bit, and print the conclusion to the screen.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void){
	unsigned int num;
	int bit;

	printf("Please enter a positive integer and the bit you would like to check: (0 is the right most bit) ");
	scanf("%d %d", &num, &bit);

	checkBit(num,bit);

	//Print out the result here

	return 0;
}

//Write your function here

Activity 32.3.

Write a C program to count the number of 1s in the binary representation of a number. Ask the user for an integer, pass it to a function countBits() that counts the set bits and returns this count, and print the result in main().

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void){
	unsigned int number;
	int numbits;

	printf("Please enter a positive integer: " );
	scanf("%d", &number);

	numbits = countBits(number);

	//Print out the result here

	return 0;
}

//Write your function here