check Least Significant Bit (LSB) of a number

We use Bit wise AND & operator to check status of any bit. Bitwise AND & operator evaluate each bit of the resultant value as 1, if corresponding bits of both operands are 1.

To check LSB of a number we need to perform bitwise ANDing. The bitwise AND operation number & 1 will evaluate to 1 if LSB of number is set i.e. 1 otherwise evaluates to 0.

Program

#include<stdio.h>
int main() {
    int n;
    scanf("%d",&n);
    if(n & 1)
        printf("LSB of %d is set (1).", n);
    else
        printf("LSB of %d is unset (0).", n);
}

Input:
12
Output
LSB of 12 is unset (0).

No comments:

Post a Comment