Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Wednesday, July 15, 2020

C Program to check LSB status of number

C Program to check least significant bit (LSB) of a number
By Ahmad Irshad | C Programming Tutorial > Bitwise operator 

Write a C program to take a number as input from user and check whether the least significant bit (LSB) of that number is set (if LSB is 1) to or not (if LSB is 0). How to check status of LSB and of a number using bitwise AND (&) operator and what is the logic are given below. 

Remember: Least significant bit (LSB) is also known as right most bit which is the bit position in binary sequence.

LSB of a number | Protec

Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.

  1. INPUT

  2. Enter first number: 10
  3. Enter second number: 20
  OUTPUT

Sum of 10 and 20 = 30

Required knowledge for this exercise

Logic to check LSB of a number using bitwise AND & operator:

As we know that the bitwise AND (&) operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1Otherwise, the corresponding result bit is set to 0

Visit to know more about bitwise operator.  

To check LSB of a number we need to perform bitwise AND operation. The bitwise AND operation number & 1 will evaluate to 1 if LSB of number is set i.e. 1 otherwise evaluates to 0. See the below example.
Check LSB of a number using bitwise operator
As you can see in above image 12 & 1 evaluate to 0. Since, LSB of 12 is 0. Whereas, 15 & 1 evaluate to 1 since LSB of 15 is 1.

Program to to check least significant bit LSB of a number: 

  1. /* C program to check LSB of a number */ #include <stdio.h> int main() { int num; /* Input a number from user */ printf("Enter any number: "); scanf("%d", &num); /* If (num & 1) evaluates to 1 */
  2. * Note: If (num & 1) is equivalent to If (num & 1 == 1)
  3. */ if(num & 1) printf("LSB of %d is set (1).", num); else printf("LSB of %d is not set i.e (0).", num); return 0; }

Output:

  1. Enter any number: 12
  2. LSB of 12 is not set i.e (0)

As you can see in above image,12 & 1 evaluate to 0. Since, LSB of 12 is 0. similarly you can check LSB of any number.





Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us