How to Set Bits of a Number in C

Embedded C for microcontrollers

In this article, we go over how to set bits of a number using the OR 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 2 LEDs are turned on. This would be represented by the binary number, 00000011.

Now let's say that we wanted to then turn the next 4 bits on. We would need to manipulate the bits so that the binary number is changed to, 00111111.

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.

Setting bits is a very common task when working with embedded microcontrollers because they allow us to turn on devices such as LEDs.

So how is this done?

Setting bits is done through the OR bitwise operation.

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

OR bitwise operation

OR bitwise operation is great for setting bits because any bit ORed with a 1 will produce a 1, which sets the bit.

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

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

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

Now let's say we want to turn the next 4 LEDs on. How can we do this?

So we have the current binary number of, 00000011

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

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

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

We want to turn on the next 4 bits of the LEDs; therefore, we have these as 1s in the mask value.

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 2 LEDs on originally).

mask1 represents the number we need in order to set 4 more bits on (to get the next 4 LEDs to turn on).

output represents the output needed to have all 6 LEDs on. This will be the new value needed to turn on 4 additional LEDs in addition to the other 2 LEDs that were on.

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.

0x3f is the hexadecimal number for the output. In binary, this is, 00111111.

And this is how to set bits using the OR bitwise operation in C.

Related Resources

How to Clear Bits of a Number in C

How to Toggle Bits of a Number in C

HTML Comment Box is loading comments...