﻿ How to Clear Bits of a Number in C ﻿

# How to Clear Bits of a Number in C

In this article, we go over how to clear bits of a number using the AND bitwise operation in C.

Embedded C is a language in C where it's important to manipulate bits of a number.

Let's say there is an 8-bit port that is connected to LEDs in a microcontroller.

Let's say that the first 4 LEDs are turned on. This would be represented by the binary number, 00001111.

Now let's say that we wanted to then turn off the last 3 LEDs and only keep one LED, the first LED, on. We would need to manipulate the bits so that the binary number is changed to, 00000001.

When working with embedded C, it is important to be able to manipulate bits, including setting bits, which is changing bits to a value of 1 or turning the bit on or giving the bits a HIGH voltage and clearing bits, which is changing bits to a value of 0 or turning the bits off or giving the bits a LOW voltage.

In this case, we want to clear 3 of the bits, which means setting 3 bits that are 1 to 0. Clearing bits is a very common task when working with embedded microcontrollers because they allow us to turn off devices such as LEDs.

So how is this done?

Clearing bits is done through the AND bitwise operation.

To understand this, let's look at the truth table for AND bitwise operation.

 AND bitwise operation Input Input Output 0 0 0 0 1 0 1 0 0 1 1 1

AND bitwise operation is great for clearing bits because any bit ANDed with a 0 will produce a 0, which clears the bit. All we have to do is take a bit AND it with a 0 and we get an output of 0, which is clearing the bit.

You can see in the above truth table that any bit ANDed with a 0 will produce an output of 0.

So let's go back to our previous example with clearing bits to operate LEDs.

Let's say that we have an 8-bit port and we have the first 4 LEDs on, which would mean a binary value of, 00001111

Now let's say we want to turn off the last 3 LEDs. How can we do this?

So we have the current binary number of, 00001111

We then need to AND this binary number with another binary number in order to produce a binary number of, 00000001.

The binary number you AND the original binary number with to produce an intended binary number is called a mask number of value. Remember that all you need to clear a bit is an AND operation with a bit value of 0.

Therefore, the mask value we need to do this is, 11110001

We want to turn off the last 3 bits of the LEDs that are on; therefore, we have these as 1s in the mask value. With this setup, the bits 1, 2, and 3 will be cleared and bits 0, 4, 5, 6, and 7 will remain the same. If the bit was 0, it will remain 0. If the bit was 1, it will remain 1.

Below is the complete code to achieve this in C.

Let's now review this code.

So we create 3 variables of type int32_t: num1, mask1, and output.

num1 represents our original binary value (with 4 LEDs on originally).

mask1 represents the number we need in order to turn off the last 3 LEDs and only keep the first LED on.

output represents the output needed to have only the first LED on. This will be the new value needed to have only the first LED on and have the other 3 LEDs turned off.

We then output the output value as a hexadecimal number. printf does not have a built-in function to output binary numbers and would need a custom function to do so.

The output from this program is shown below.

0x1 is the hexadecimal number for the output. In binary, this is, 00000001.

And this is how to clear bits using the AND bitwise operation in C.

Related Resources

How to Set Bits of a Number in C

How to Toggle Bits of a Number in C

HTML Comment Box is loading comments...

﻿