C Program to Check if Bit Position is Set to One or not

This is a C Program to check if a given bit position is set to one or not.

Problem Description

This C Program checks if a given bit position is set to one or not.

Problem Solution

Take input from the user and checks bit position as shown in the program below.

Program/Source Code

Here is source code of the C Program to check if a given bit position is set to one 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 a given Bit Position is set to One or not
 */
#include <stdio.h>
 
void main()
{
    unsigned int number;
    int result, position;
 
    printf("Enter the unsigned integer:\n");
    scanf("%d", &number);
    printf("enter position to be searched\n");
    scanf("%d", &position);
    result = (number >> (position));
    if (result & 1)
        printf("TRUE\n");
    else
        printf("FALSE\n");    
}
Program Explanation

In this C Program, we are reading the unsigned integer and position to be searched using ‘number’ and ‘position’ variables respectively. Compute the Binary Right Shift Operation.

advertisement
advertisement

The left operand value is moved right by the number of bits specified by the right operand. If else condition statement is used to copy a bit to the result if it exists in both operands using Binary AND operator. Print the bit position which is set to one or not.

Runtime Test Cases
 
$ cc bit14.c
$ a.out
Enter the unsigned integer:
128
enter position to be searched
7
TRUE

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]

advertisement
advertisement
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.