How to Clear All Bits of a Register in C

Embedded C for microcontrollers



In this article, we go over how to clear all bits of a register in C.

Clearing bits can be important for a variety of reasons including starting the register off with a clean slate or to put a register all as inputs (since many microcontrollers declare inputs as 0s).

Clearing bits can be done by using the & (AND) bit operator with the ~ (NOT) symbol with bits it is ANDed with as 1s.

We show how to do this with 8-bit, 16-bit, and 32-bit registers.

After this, you will have the know how to do this with registers of any bit sizes.

How to Clear All Bits of an 8-Bit Register

We will first show how to clear all bits of an 8-bit register.

Below is the code to do so.





Let's go over this code now.

We create an 8-bit register, named *pRegister1, which holds an 8-bit number in hexadecimal, 0xE2.

An 8-bit register has 8 bits.

Hexadecimal numbers go from 0 to F (15). 15 in bits is 1111. So hexadecimal covers 4 bits at a time. Thus, you see 2 digits for an 8-bit number.

To now clear the bits of an 8-bit number, we use the line, *pRegister1 &= ~(0xFF);

When ANDing a register with the ~ (NOT) of F's in hexadecimal, this is the equivalent of clearing the bits.

If you were clearing the bits in binary, the line would be, *pRegister1 &= ~(0b11111111);

Maybe this helps to illustrate it better. By ANDing a register with the NOT of 1s, this is the equivalent to making all the bits in the register 0s.

However, hexadecimal is shorter and requires fewer digits, so it can be seen as preferred.

How to Clear All Bits of a 16-Bit Register

We will now show how to clear all bits of a 16-bit register.

Below is the code to do so.





Let's go over this code now.

We create a 16-bit register, named *pRegister1, which holds a 16-bit number in hexadecimal, 0xA4E2.

A 16-bit register has 16 bits.

Hexadecimal numbers go from 0 to F (15). 15 in bits is 1111. So hexadecimal covers 4 bits at a time. Thus, you see 4 digits for a 16-bit number.

To now clear the bits of a 16-bit number, we use the line, *pRegister1 &= ~(0xFFFF);

When ANDing a register with the ~ (NOT) of F's in hexadecimal, this is the equivalent of clearing the bits.

How to Clear All Bits of a 32-Bit Register

We will now show how to clear all bits of a 32-bit register.

Below is the code to do so.





Let's go over this code now.

We create a 32-bit register, named *pRegister1, which holds a 32-bit number in hexadecimal, 0x46BCA4E2.

A 32-bit register has 32 bits.

Hexadecimal numbers go from 0 to F (15). 15 in bits is 1111. So hexadecimal covers 4 bits at a time. Thus, you see 8 digits for a 32-bit number.

To now clear the bits of a 32-bit number, we use the line, *pRegister1 &= ~(0xFFFFFFFF);

When ANDing a register with the ~ (NOT) of F's in hexadecimal, this is the equivalent of clearing the bits.

And this is how to clear all bits of a register in embedded C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C



HTML Comment Box is loading comments...