# How to check a particular bit is SET or not using C program?

Learn: **How to check whether a bit is SET (High) or not using C programming language?** Here, we will read a number and bit and check input bit is SET or not.

**Bitwise AND Operator (&) is used to check whether a bit is SET (HIGH) or not SET (LOW)** in C and C++ programming language.

**Bitwise AND Operator (&)** is a binary operator, which operates on two operands and checks the bits, it returns 1, if both bits are SET (HIGH) else returns 0.

Let suppose, you want to check Nth bit of a Number NUM, you can do the same by following this syntax:

(NUM & (1<<N))

Here, NUM is the number whose bit you want to check and N is the bit number, (1<<N) SET the particular bit at N^{th} position.

**Example:** Statement (1<<3) returns 8 (in Binary: 0000 1000), **see the binary 3 ^{rd} (count from 0 to 7) bit is SET here**.

**In this program:**

Input: Enter an 8 bits integer number: 31 Now, enter a bit number (from 0 to 7) to check, whether it is SET or not: 3 Output: Bit number 3 is SET in number 31

## Program to check whether bit is SET or not in C language

#include <stdio.h> int main() { int NUM; //to store number int N; //to store bit printf("Enter an 8 bits integer number: "); scanf("%d",&NUM); printf("Now, enter a bit number (from 0 to 7) to check, whether it is SET or not: "); scanf("%d",&N); //checking bit status if(NUM & (1<<N)) printf("Bit number %d is SET in number %d.\n",N,NUM); else printf("Bit number %d is not SET in number %d.\n",N,NUM); return 0; }

Output

Enter an 8 bits integer number: 31 Now, enter a bit number (from 0 to 7) to check, whether it is SET or not: 3 Bit number 3 is SET in number 31.

Here, the input was 31, which is 0001 1111 and its 3^{rd} bit is SET.

**See the image:**

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