Set, Clear, Toggle Bit, and Check a single Bit in C Language

set-clear-toggle-bit-and-clear-bit-in-c-programming-language

Set, Clear, Toggle bit, and Check bit Introduction:

We have looked at the Bitwise operators in C language and explained them with Examples in our earlier articles, In today’s article, We are going to look at a few uses of the bitwise operators, Like how to Set, Clear, Toggle, and Check a Bit in C language using bitwise operators.

Set, Clear, Toggle bit, and Check bit in C Pre-Requisites:

It is recommended to know how the bitwise operators work in C language. Please go through the following link to learn more about them.

Let’s start with the Set a bit in C.

Set a Bit in C ( Set Nth bit) using bitwise Operators:

To set a bit or Nth bit in C language, We use the Bitwise OR operator with the bitwise Left shift operator.

The SETting an Nth bit means, We need to make the Nth bit as One(1). So if the bit in the Nth position is Zero(0), Then we need to change it to One(1). If the bit at the Nth position is One(1), We don’t do anything and the bit will have the value as One(1).

Logic to Set Nth bit or Set a bit:

To understand the logic of Set Nth bit, We need to know the truth table of OR. Here is the truth table of bitwise OR

Bit-1Bit-2OR (Bit- 1 | Bit-2)
000
101
011
111

To Set the Nth bit, Use the following bitwise OR logic.

InputNumber |= ( 1 << bitPosition )

with is equal to

InputNumber = InputNumber | (1 << bitPosition)

Here,
The InputNumber is the number where we are trying to SET the bit
The bitPosition is the position of the bit (Nth bit) in the binary sequence of InputNumber

So we are shifting the number 1, The bitposition times in the left-side direction. Then performing the bitwise OR operator.

Set Nth Bit Example:

Let’s look at an example: Set the 2nd bit in number 120.

InputNumber = 120.

bitPosition = 2

The binary equivalent of the number 120 is 00000000000000000000000001111000

Now, To set the bit at position 2. The bit position starts with index 0, So the Least significant bit(LSB) in the above binary sequence position is 0.

To Set the 2nd bit in the number 120, Use.

InputNumber = InputNumber | (1 << 2);

InputNumber = 120 | (1 << 2);

Here is the calculation

➡️ Bitwise OR : (InputNumber | (1 << 2)) is:
–> 0111 1000   (InputNumber
120 in Binary)
–> 0000 0100  (1 << 2) 
–> 0111 1100   ( Result = (InputNumber | (1 << 2)) which is equal to 124 )

After the above operation, The bit at the 2nd position of the InputNumber(120) is changed from zero(0) to One(1). So we are able to SET the 2nd bit and The InputNumber value became 124.

The final value of InputNumber is 124 ( Which is 00000000000000000000000001111100 in binary )

📢 The Bit positions start with the Index 0 ( from the left side). So when we SET the bit at Index 2, We actually changed the 3rd from LSB(If you take Index as 1).

Set Nth Bit in C Program:

Let’s look at the set Nth bit algorithm, Then we will look at the program as well.

Set Nth bit Algorithm:

  1. Take the Input from the user and store it in variable num
  2. Get the bit number to set from the user and store it in variable bit
  3. Take backup of num to variable called backup for later use
  4. Now Set the bit using the above bitwise OR logic i.e num = num | (1 << bit);
  5. Display the results on the console.

Set Nth bit Program in C Language:

Program Output:

Let’s compile and run the program using your compiler.

gcc set-bit.c

Example 1: InputNumber 120 and BitPosition 2.

As you can see from the above output, We are getting the expected results – 124.

Let’s check the output for a few more cases

set-nth-bit-in-c-language-with-bitwise-OR-program

As expected, We are getting the desired results. And the program is able to SET the bits properly.

Check Nth Bit in C language:

The check Nth bit means We need to check if the bit at the Nth position is One or Zero. This check Nth bit logic will help us to convert decimal number to binary number.

We have already covered the Decimal to Binary Number conversion program, We have used the Check Nth bit logic there as well.

To check Nth bit, We use the bitwise AND operator with the bitwise left shift operator. Here is the logic to check Nth bit.

( InputNumber & (1 << bitPosition) )

Here, InputNumber is the given number and bitPosition is the Nth bit ( The bit we want to check)

So we are left shifting the 1, bitPositions times and performing the bitwise AND operation with InputNumber

Check Nth bit Example:

Let’s look at an example to understand how the check Nth bit logic works.

InputNumber = 45

bitPosition = 3

Apply the above logic.

( InputNumber & (1 << bitPosition) ) ? 1 : 0

(45 & ( 1 << 3)) ? 1 : 0;

➡️ (45 & (1 << 3)) ? 1 : 0:
–> 0010 1101   (InputNumber
45 in Binary)
–> 0000 1000  (1 << 3) 

Now if you observe the above binary sequence(of number 45), The bit at position 3 is 1 (SET). So we will get the result as SET i.e 1.

Program to Check Nth Bit is SET or CLEAR:

Check Nth Bit Algorithm:

  1. Take the input number and bit number from the user and store them in the variables num and bit respectively.
  2. We won’t allow negative numbers, So check for negative numbers and display an error message
  3. Check the bit using the check Nth logic – result = (num & (1 << bit)) ? 1 : 0;
  4. If the result is 1, Then bit is SET i.e one(1), Otherwise bit is CLEAR or Not-Set. i.e Zero(0).

Check Nth bit Program in C:

Program Output:

Run the program

check-nth-bit-in-c-using-bitwise-And-operator-output

As we can see from the above output, We are getting the expected results.

Clear Nth Bit in C Programming:

To CLEAR a bit or CLEAR Nth bit in C language, We use the Bitwise AND operator with the bitwise Left shift operator and One’s complement Operator.

The CLEARing an Nth bit means, We need to make the Nth bit as Zero(0). So if the bit in the Nth position is One(1), Then we need to change it to Zero(0). If the bit at the Nth position is Zero(1), We don’t do anything and the bit will have the value as Zero(0).

Logic to Clear Nth bit or Clear a bit:

To understand the logic of Clear Nth bit, We need to know the truth table of AND. Here is the truth table of bitwise AND

Bit-1Bit-2AND (Bit- 1 & Bit-2)
000
100
010
111

To CLEAR the Nth bit, Use the following bitwise AND logic.

InputNumber &= ~( 1 << bitPosition )

with is equal to

InputNumber = InputNumber & (~ (1 << bitPosition))

Here,
The InputNumber is the Given Number
The bitPosition is the position of the bit (Nth bit) in the binary sequence of InputNumber

So we are shifting the number 1, The bitposition times in the left-side direction and Applying the one’s complement. Finally performing the bitwise AND operation.

CLEAR Nth Bit Example:

Let’s look at an example: Set the 4th bit in number 63.

InputNumber = 63.

bitPosition = 4.

The binary equivalent of the number 63 is 00000000000000000000000000111111

To set the 4th bit in the number 63, Use.

InputNumber = InputNumber & ~ (1 << 4);

InputNumber = 120 & ~ (1 << 4);

Here is the calculation

➡️ (InputNumber & ~(1 << 4)) is:

–> 0001 0000  (1 << 4) 

–> 1110 1111  ~(1 << 4) 

–> 0011 1111   (InputNumber63 in Binary)

–> 0010 1111   ( Result = (InputNumber & ~(1 << 4)) which is equal to 47 )

After the above operation, The bit at the 4th position of the InputNumber(63) is changed from One(1) to Zero(0).

So we are able to CLEAR the bit at position 4 using the InputNumber = InputNumber & ~ (1 << bit); Logic.

The final value of InputNumber is 47 ( Which is 00000000000000000000000000101111 in binary )

CLEAR Nth bit Program in C Language:

Clear Nth bit Algorithm:

  1. Accept the input number from the user and store it in variable num, Also take the bit position and store it in bit variable.
  2. Check for negative numbers and display an error notice to the user.
  3. Take a backup of the num to backup variable for later usage.
  4. CLEAR the bit using the Clear Nth bit logic – num = num & ~(1 << bit);
  5. Display the Results.

Clear Nth bit in C Program using Bitwise AND Operator:

Program Output:

Compile and Run the Program.

clear-nth-bit-program-in-c-output

As you can see from the above output, We are getting the results correctly, We also added logic to display an error on a negative number.

Toggle Nth Bit or Toggle a Bit in C Programming:

Toggling Nth Bit means, If the Nth bit is One(1), Then we need to Invert it and make it Zero(0). Similarly, If the Nth bit is Zero(0), We need to change it to One(1).

To Toggle Nth bit, We use the Bitwise XOR operator with the bitwise Left shift operator.

Logic to Toggle Nth bit or Invert a bit:

To understand the logic of the Toggle Nth bit, We need to know the truth table of XOR.

Bit-1Bit-2XOR (Bit- 1 ^ Bit-2)
000
101
011
110

To Toggle the Nth bit, Use the following bitwise XOR logic.

InputNumber ^= ( 1 << bitPosition )

with is equal to

InputNumber = InputNumber ^ (1 << bitPosition)

Here,
The InputNumber is the Given Number
The bitPosition is the position of the bit (Nth bit) in the binary sequence of InputNumber

So we are shifting the number 1, The bitposition number of positions in the left-side direction and Performing the bitwise XOR operation.

📢 Bitwise XOR Operator is used to Invert or Toggle a Bit

Toggle Nth Bit Example:

Let’s look at an example: Set the 4th bit in number 38.

InputNumber = 38.

bitPosition = 4.

The binary equivalent of the number 63 is 00000000000000000000000000100110

To set the 4th bit in the number 38, Use.

InputNumber = InputNumber ^ (1 << 4);

InputNumber = 38 ^ (1 << 4);

Here is the calculation

➡️ (InputNumber ^ (1 << 4)) is:

–> 0010 0110   (InputNumber38 in Binary)

–> 0001 0000  (1 << 4) 

–> 0011 0110   ( Result = (InputNumber ^ (1 << 4)) which is equal to 54 )

We have toggled the 4th bit in the number 38 using the bitwise XOR operator.

The bit at the 4th position i.e zero(0), has been Toggled/inverted and changed to One(1).

The final value of InputNumber is 54 ( Which is 00000000000000000000000000110110 in binary )

Toggle Nth Bit Program in C Language:

Toggling Nth bit Algorithm:

  1. Take the input number num and bit position bit from the user.
  2. Display error on negative numbers
  3. Take back up of the num and store it in a variable named backup
  4. Now Toggle the bit using the Toggle Nth bit logic – num = num ^ (1 << bit);
  5. Print the results onto the console.

Toggling Nth bit Program in C using bitwise Operators:

Let’s convert the above pseudo code into the C program.

Program Output:

We are compiling the program using the GCC compiler.

Verify the results of the program

As you can see from the above output, We are getting the correct results and the program is able to toggle the bit at the specified position.

Toggle-bit-in-c-language-with-example-program

Let’s check a few more examples.

Set, Clear, Toggle bit, and Clear bit in C Programs Conclusion:

In this article, We have learned about the Set, Clear, Toggle bit, and clear a bit in C language. We looked at the example for each section with detailed step-by-step explanations, Finally, we have written a C program for each section to make our understanding concrete.

If you have reached here, Then Thank you for taking the time to check out the full article. Here are similar programs to set, clear, toggle bit, and a clear bit in the C program.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Set, Clear, Toggle Bit, and Check a single Bit in C Language 🆕 […]

Leave a Reply