C Program to Check whether nth Bit is Set or not

This is a C Program to check if nth bit in a 32-bit integer is Set or not.

Problem Description

This C Program checks if nth bit in a 32-bit integer is set or not.

Problem Solution

Take input from the user and checks whether the position is set or not as shown in the program below.

Program/Source Code

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)
/* function to check whether the position is set to 1 or not */
int n_bit_position(int number,int position)
    result = (number>>(position));
Program Explanation

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.

Runtime Test Cases
$ cc bit32.c
$ a.out
Enter the unsigned integer:
enter position
$ a.out
Enter the unsigned integer:
enter position

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.