# How to set, clear and toggle a single bit in C language?

Here, we are going to learn **how do you set, clear and toggle a single bit of a number in C programming language**?

Submitted by **IncludeHelp**, on February 14, 2019

**Given a number and we have to 1) set a bit, 2) clear a bit and 3) toggle a bit.**

### 1) Setting a bit

To set a particular bit of a number, we can use bitwise OR operator (|), it sets the bits – if the bit is not set, and if the bit is already set, bit’s status does not change.

Let suppose there is a number num and we want to set the X^{th} bit of it, then the following statement can be used to set X^{th} bit of num

num |= 1 << x;

### 2) Clearing a bit

To clear a particular bit of a number, we can use bitwise AND operator (&), it clears the bit – if the bit is set, and if the bit is already cleared, bit’s status does not change.

Let suppose there is a number num and we want to clear the X^{th} bit of it, then the following statement can be used to clear X^{th} bit of num

num &= ~(1 << x);

### 3) Toggling a bit

To toggle a particular bit of a number, we can use bitwise XOR operator (^), it toggles the bit – it changes the status of the bit if the bit is set – it will be un-set, and if the bit is un-set – it will be set.

Let suppose there is a number num and we want to toggle the X^{th} bit of it, then the following statement can be used to toggle X^{th} bit of num

num ^= 1 << x;

**Consider the following code – Here, we are performing all above mentioned operations**

#include <stdio.h> int main(){ //declaring & initializing an 1 byte number /* num (hex) = 0xAA its binary = 1010 1010 its decimal value = 170 */ unsigned char num = 0xAA; printf("num = %02X\n", num); //setting 2nd bit num |= (1<<2); //now value will be: 1010 1110 = 0xAE (HEX) = 174 (DEC) printf("num = %02X\n", num); //clearing 3rd bit num &= ~(1<<3); //now value will be: 1010 0110 = 0xA6 (HEX) = 166 (DEC) printf("num = %02X\n", num); //toggling bit 4th bit num ^= (1<<4); //now value will be: 1011 0110 = 0xB6 (HEX) = 182 (DEC) printf("num = %02X\n", num); //toggling bit 5th bit num ^= (1<<5); //now value will be: 1001 0110 = 0x96 (HEX) = 150 (DEC) printf("num = %02X\n", num); return 0; }

**Output**

num = AA num = AE num = A6 num = B6 num = 96

**Read more...**

- Hexadecimal literals in C language
- Working with hexadecimal values in C language
- How to check whether a bit is SET or not in C language?
- unsigned char in C language
- Bitwise operator programs in C

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions