﻿ How to Test a Bit of a Number in C ﻿

# How to Test a Bit of a Number in C

In this article, we go over how to test a bit of a number in C.

Testing a bit of a number is important in circuits when you need to read a value from a variable.

For example, you may have a microcontroller connected to a keypad. Each key of the keypad represents a different binary number because each has its own code. In order to know which key is pressed, you have to be able to test a bit of a number.

Let's take a simple example below.

Let's say we have the number, 0b1001

In hex, this number is, 0x09

In order to be able to test each bit of a number we use an if statement with the original number with the & (AND) operator with the number 1 left shifted to the bit that you want to test.

The full program of this testing of each of the bits of this 4 bit number is shown below.

Let's now go over the code.

So we create an 8 bit integer variable, number1, which holds a 4-bit number. We represent here in binary but it could also be represented in any other number format such as hexadecimal. In binary, it is, 0b1001. In hexadecimal, it is, 0x09.

The first thing we do is test the first bit, which is bit 0.

The if statement we use is, if (number1 & (1 << 0)){ }

What this statement does is take the original number, number1, and takes the & (AND) operator, and then takes 1 and left shifts it 0 positions. Since we're left shifting it 0 positions, it could also be written, if (number1 & 0){ }

This line of code takes the bit 0 of number1 and ANDs it with 1. If the bit is 1, then the output is 1. If the bit is 0, then the output is 0. Thus, we can use the & operator to determine if a specific bit is 1 or 0.

Next, we test the second bit, which is bit 1.

The if statement to test the bit 1 is, if (number1 & (1 << 1))

(1 << 1) left shifts a 1 into bit 1, which is the second bit.

So bit 1 of the variable number 1 is ANDed with a 1. If the second bit is 1, the output is 1. If the second bit is 0, then the output is 0.

Next, we test the third bit, which is bit 2.

The if statement to test the bit 1 is, if (number1 & (1 << 2))

(1 << 2) left shifts a 1 into bit 2, which is the third bit.

So bit 2 of the variable number 1 is ANDed with a 1. If the third bit is 1, the output is 1. If the third bit is 0, then the output is 0.

Lastly, we test the fourth bit, which is bit 3.

The if statement to test the bit 1 is, if (number1 & (1 << 3))

(1 << 3) left shifts a 1 into bit 3, which is the fourth bit.

So bit 3 of the variable number 1 is ANDed with a 1. If the fourth bit is 1, the output is 1. If the fourth bit is 0, then the output is 0.

So this is how we can test any bit of a number in C.

The output from this program is shown below.

So you can see that the output gets all of the numbers correct for each of the bits.

Testing bits is very important when taking in user input.

For example, below is the code to determine which key a user pressed from a 4x4 keypad with a STM32F407G discovery board with C.

We use a series of if statements with an input data register and left shifting 1 into specific bits in order to test individual bits of the input data register to see which key is pressed.

This is the importance of being able to test bits of a number to be able to get input from a user.

And this is how to test a bit of a number in C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C