How to Set All Bits of a Register in C

Embedded C for microcontrollers



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

Setting bits can be important for a variety of reasons including turning on all bits of a register or to put a register all as outputs (since many microcontrollers declare outputs as 1s).

Setting all bits can be done by using the | (OR) bit operator with 1s for each of the bits. This is because 1 OR with any number sets the number as 1.

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 Set All Bits of an 8-Bit Register

We will first show how to set 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 set the bits of an 8-bit number, we use the line, *pRegister1 |= 0xFF;

When ORing a register with the F's in hexadecimal, this is the equivalent of setting the bits.

If you were setting the bits in binary, the line would be, *pRegister1 |= 0b11111111;

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

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

How to Set All Bits of a 16-Bit Register

We will now show how to set 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 set the bits of a 16-bit number, we use the line, *pRegister1 |= 0xFFFF;

When ORing a register with the F's in hexadecimal, this is the equivalent of setting the bits.

How to Set All Bits of a 32-Bit Register

We will now show how to set 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 set the bits of a 32-bit number, we use the line, *pRegister1 |= 0xFFFFFFFF;

When ORing a register with the F's in hexadecimal, this is the equivalent of setting the bits.

And this is how to set 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...