Program to check if Least Significant Bit for a number is set or not

7:10 PM 1 Comments A+ a-

In Binary number representation of a number, the rightmost bit (or digit) is called the Least Significant Bit (LSB) and the leftmost bit is called the Most Significant Bit (MSB). As we know while converting a binary representation of a number back to its decimal system representation, we calculate the decimal representation by multiplying the LSB by 20 and then increasing the power of 2 from 0 by 1 on each bit as we go from LSB to MSB.
For example: Suppose we have a binary number: 11001
It's decimal equivalent will be: 1*24 + 1*23 + 0*22 + 0*21 + 1*20 = 25
Let us take another example:
Binary number: 11100
It's decimal equivalent will be: 1*24 + 1*23 + 1*22 + 0*21 + 0*20 = 28

Thing to note from the above discussion

Sometimes in the interviews you do not get the questions directly and to even understand the question properly, you need to have a careful look at the question statement. 
Suppose, like in this case, you are asked to write a program to check if the Least Significant Bit of a number is set or not (And you can not make use of the % and / operators. 
Here in this question, if you did not get what the interviewer is expecting from you, you might end up wasting a lot of time calculating the binary equivalent of the number and then manually checking the value of its LSB. But thankfully, this is not what this questions expects you to do. Let us go back to the first section of the post where we discussed in brief about the binary to decimal conversions. The one and only thing I wanted you to notice from the above discussion is that, for even numbers, the LSB is not set and for odd, the LSB is always set. 
Now understanding this simple logic can save you a lot of efforts in your interviews and coding competitions. Now you would have understood why the examiner has denied you the use of '%' and '/' operators. So, you are basically assigned a challenge of finding whether a given number is odd or even without using the '%' and '/' operators
There might be many approaches to do that, I will just implement only one in the below Java program method. The method will simply take the input number and return true or false based on whether the LSB is set or not.

public boolean isLSBSet(int number) {

  String num = Integer.toString( number );
  int lastDigit = num.length() - 1;
  //Only for odd numbers, the Least Significant Bit will be set.
  if (num.charAt(lastDigit) == '1' || num.charAt(lastDigit) == '3'
    || num.charAt(lastDigit) == '5' || num.charAt(lastDigit) == '7'
    || num.charAt(lastDigit) == '9')
   return true;
  else
   return false;
 }



We thank DJ Spiess for his tip! The whole above code now can be reduced to one line.

return (number ^ (number - 1)) == 1;
So, Kudos! We now know How to check if number is even or odd without using the modulus '%' or '/' operator. Keep sharing such tips to the community and help us all learn Java in a simple and fun way! :)

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!

1 comments:

Write comments
ERP
AUTHOR
March 21, 2017 at 12:51 PM delete

ERPTREE is a leading oracle fuison HCM Training Institute. we offer this course through online we have great experience in succeeding students through online courses. we can calculate our performance through their honest comments in our sites in supporting our services. we have referral program so candidates can earn money through referral. you can share your live experience with other can generate you some money.


Oracle Fusion HCM Training

Reply
avatar