This is a C Program to check if nth bit in a 32-bit integer is Set or not.
This C Program checks if nth bit in a 32-bit integer is set or not.
Take input from the user and checks whether the position is set or not as shown in the program below.
Here is source code of the C Program to check if nth bit in a 32-bit integer is set or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Check if nth Bit in a 32-bit Integer is Set or not */ #include <stdio.h> /* gloabal varaibles */ int result,position; /* function prototype */ int n_bit_position(int x,int position); void main() { unsigned int number; printf("Enter the unsigned integer:\n"); scanf("%d", &number); printf("enter position\n"); scanf("%d", &position); n_bit_position(number, position); if (result & 1) printf("YES\n"); else printf("NO\n"); } /* function to check whether the position is set to 1 or not */ int n_bit_position(int number,int position) { result = (number>>(position)); }
In this C Program, we are reading the unsigned integer and position using ‘number’ and ‘position’ variables respectively. The n_bit_position() function is used to check whether the position is set to 1 or not.
The result variable is used to perform Binary Right Shift Operator, the left operand’s value is moved right by the number of bits specified by the right operands.
If else condition statement is used to check that value of ‘result’ variable consists of 1 bit. If the condition is true, then execute the statement and print the output of the program.
$ cc bit32.c $ a.out Enter the unsigned integer: 101 enter position 4 NO $ a.out Enter the unsigned integer: 113 enter position 4 YES
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Check C Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for C Internship
- Check Computer Science Books