﻿ How to Toggle Bits of a Number in C ﻿

# How to Toggle Bits of a Number in C

In this article, we go over how to toggle bits of a number using the XOR 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 those 4 LEDs and turn on the other 4 LEDs. We would need to manipulate the bits so that the binary number is changed to, 11110000.

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, and toggling bits, which is changing 0s to 1s and 1s to 0s.

In this case, we want to turn off the 4 LEDs that are on and turn on the other 4 LEDs.

So how is this done?

Toggling bits is done through the XOR bitwise operation.

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

 XOR bitwise operation Input Input Output 0 0 0 0 1 1 1 0 1 1 1 0

XOR bitwise operation is great for toggling bits because with the right XOR operation, the bit can be toggled, a 1 to a 0 or a 0 to a 1. In order to change a 1 to a 0, we XOR the 1 with another 1, which then produces an output of 0. In order to change a 0 to a 1, we XOR the 0 with a 1, which then produces an output of 0. In order to keep a 1 unchanged, we XOR the 1 with a 0. In order to keep a 0 unchanged, we XOR the 0 with a 0.

So let's go back to our previous example with toggling 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 those 4 LEDs and turn on the other 4 LEDs. How can we do this?

So we have the current binary number of, 00001111

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

The binary number you XOR the original binary number with to produce an intended binary number is called a mask number of value.

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

This will toggle the 0s to 1 and the 1s to 0.

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 turn off the 4 LEDs and turn on the other 4 LEDs.

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.

0xf0 is the hexadecimal number for the output. In binary, this is, 11110000.

And this is how to toggle bits using the XOR bitwise operation in C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C