C program to check whether all bits of a number are UNSET/LOW?
Give a number of one byte (8 bits) and we have to check whether all bits are UNSET/LOW.
Example -1
Input number: 0
Binary value: 00000000
Output: Yes, all bits are unset
Example -2
Input number: 50
Binary value: 00110011
Output: No, all bits are not unset
To solved this program, we will use BITWISE AND (&) operator, we will traverse bits from 7 to 0 (in case of two bytes number, it would be 15 to 0 and so on...) and check if there is any SET/HIGH bit, then we will break the loop and output will be false that means "all bits are not UNSET/LOW".
Consider the program:
Output
We wrote a function int isAllBitsUnset(unsigned int num), it is taking an unsigned integer type number as an argument and returning 0 if all bits are not UNSET/LOW and 1 if all bits are UNSET/LOW.
function:
To check the bits, we are traversing loop from 7 to 0 (in case of 8 bits) and checking each bit, whether it is HIGH/SET or not, if any bit is HIGH/SET then value of cnt will be 1 (which is 0 initially, cnt is using as a flag variable) and loop will break.
need an explanation for this answer? contact us directly to get an explanation for this answer