Learn Programming and technology with ease @ developerpublish.com

HomeC ProgrammingC Program to Check whether nth Bit is Set or not

# C Program to Check whether nth Bit is Set or not

In computer programming, bitwise operations are frequently used to manipulate individual bits in a binary representation of data. One common task is to determine whether a specific bit at a given position is set (1) or not (0). In this post, we will explore a C program that checks whether the nth bit of a number is set or not, using bitwise operations.

## Problem statement

Given an integer number â€˜numâ€™ and a positive integer â€˜nâ€™, we need to determine whether the nth bit of â€˜numâ€™ is set (1) or not (0). The program should output the result based on the evaluation.

## C Program to Check whether nth Bit is Set or not

```#include <stdio.h>

int isNthBitSet(int num, int n) {
// Shifting 1 by n positions to the left
int mask = 1 << n;

// Bitwise AND operation to check if the nth bit is set or not
return 1; // nth bit is set (1)
else
return 0; // nth bit is not set (0)
}

int main() {
int num, n;

printf("Enter a number: ");
scanf("%d", &num);

printf("Enter the position (n) of the bit to check: ");
scanf("%d", &n);

int result = isNthBitSet(num, n);

printf("The %dth bit of %d is %s.\n", n, num, result ? "set" : "not set");

return 0;
}
```

## Input/Output

Share:

### You May Also Like

#### C Program to Find Volume and Surface Area of Sphere

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...

#### C Program to Convert Roman Number to Decimal Number

This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...

#### C Program to Calculate the Value of sin(x)

This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...