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

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 2

For example: Suppose we have a binary number: 11001

It's decimal equivalent will be: 1*2

Let us take another example:

Binary number: 11100

It's decimal equivalent will be: 1*2

^{0}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*2

^{4}+ 1*2^{3}+ 0*2^{2}+ 0*2^{1}+ 1*2^{0}= 25Let us take another example:

Binary number: 11100

It's decimal equivalent will be: 1*2

^{4}+ 1*2^{3}+ 1*2^{2}+ 0*2^{1}+ 0*2^{0}= 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! :)
## 1 comments:

Write commentsERPTREE 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.

ReplyOracle Fusion HCM Training