Lesson 3.6. Bitwise operators details

To understand the bitwise operators in C, we will study here some examples. When we want to simulate on paper operators &, | ^ and ~, we have to proceed in three steps steps:

  1. convert the operand(s) into binary
  2. perform the logical operation on each bit
  3. convert the result to decimal

Example 1

Consider the following example with the AND operator:

unsigned char a=197, b=227 , s;
s = a & b;

Step 1: convert a and b to binary (an online converter is available here ).

$$ a = (197)_{10} = (1100\:0101)_2 $$ $$ b = (227)_{10} = (1110\:0011)_2 $$

Note that we convert to 8 bits, because the unsigned char is encoded on 8 bits. If it were an int you would have to convert to 32 bits.

Step 2: Perform the AND operator on each bit:

$$ \begin{array}[t]{r} 1100\:0101 \\ \& \; 1110\:0011 \\ \hline 1100\: 0001 \end{array} $$

Step 3: Convert the result s from binary to decimal:

$$ s = (1100\:0001)_2 = (193)_{10} $$

Let's check the result:

Example 2

Let's consider the same example with theXOR operator:

unsigned char a=197, b=227 , s;
s = a ^ b;

Step 1: same binary conversion as on the previous example:

$$ a = (197)_{10} = (1100\:0101)_2 $$ $$ b = (227)_{10} = (1110\:0011)_2 $$

Step 2: perform the XOR on each bit:

$$ \begin{array}[t]{r} 1100\:0101 \\ \bigoplus \; 1110\:0011 \\ \hline 0010\: 0110 \end{array} $$

Step 3: Convert the result s from binary to decimal:

$$ s = (0010\:0110)_2 = (38)_{10} $$

Let's check the result:

Example 3

Consider the example below which inverts the variable a bit by bit:

unsigned char a=225, s;
s = ~a;

Step 1: convert a to binary:

$$ a = (225)_{10} = (1110\:0001)_2 $$

Step 2: apply the NOT operator on each bit:

$$ \begin{array}[t]{r} a &=& \; 1110\:0001 \\ \hline s = \overline{a} &=& 0001\: 1110 \end{array} $$

Step 3: Convert the result s from binary to decimal:

$$ s = (0001\:1110)_2 = (30)_{10} $$

Let's check the result:

Exercise

Complete the following program so that it calculates the NAND bit by bit of the variables a and b:

#include <stdio.h>

int main(void) {
    unsigned char a =125, b=89, s;

    // a NAND b => s
    // COMPLETE HERE

    // Display the result
    printf ("s = %d\n", s);

    return 0;
}

Quiz

To complete the quiz, you will need a paper and pencil to reproduce the steps presented in the examples in this course. You can also use this online binary/decimal converter.

What does the following program display?

unsigned char a=3, b=5;
printf ("%d", a & b );
Check Bravo! $$ \begin{array}[t]{r} 0000\:0011 &=& (3)_{10} \\ \& \; 0000\:0101 &=& (5)_{10} \\ \hline 0000\: 0001 &=& (1)_{10} \end{array} $$ Try again...

What does the following program display?

unsigned char a=3, b=5;
printf ("%d", a | b );
Check Bravo! $$ \begin{array}[t]{r} 0000\:0011 &=& (3)_{10} \\ + \; 0000\:0101 &=& (5)_{10} \\ \hline 0000\: 0111 &=& (7)_{10} \end{array} $$ Try again...

What does the following program display?

unsigned char a=3, b=5;
printf ("%d", a ^ b );
Check Bravo! $$ \begin{array}[t]{r} 0000\:0011 &=& (3)_{10} \\ \bigoplus \; 0000\:0101 &=& (5)_{10} \\ \hline 0000\: 0110 &=& (6)_{10} \end{array} $$ Try again...

What does the following program display?

unsigned char a=0xAA, s;
s = ~a;
printf ("%u", s );
Check Bravo! $$ \begin{array}[t]{r} a &=& \; 1010\:1010 &=& (AA)_{16} \\ \hline s= \overline{a} &=& 0101\: 0101 &=& (85)_{10} \end{array} $$ Try again...

What does the following program display?

unsigned char a=3, b=5, s;
s = a ^ ~b;
printf ("%u", s );
Check Bravo! $$ \begin{array}[t]{r} b &=& 0000\:0101 &=& (5)_{10} \\ \hline \overline{b} &=& 1111\: 1010 &=& (250)_{10} \end{array} $$ $$ \begin{array}[t]{r} a &=& 0000\:0011 &=& (3)_{10} \\ \bigoplus \overline{b} &=& \; 1111\:1010 &=& (250)_{10} \\ \hline s &=& 1111\: 1001 &=& (249)_{10} \end{array} $$ Try again...

See also


Last update : 05/10/2023