# Checking if a Bit is Set.

• 01-17-2012, 09:28 PM
Hypnos
Checking if a Bit is Set.
Hello.

My question is this.

Write a static method, called boolean checkBit( int x, int k ), that checks that kth bit is set. For example, checkBit( y, 2) checks that the 3rd bit is set (remember that 0 is the first bit)

I'm Really confused by this question, I don't know how to write a program that can check through the bits in anything. A part from that i don't know what it is i'm supposed to be checking. I know i'm looking for the kth bit, but in what? I'm not given a number in binary, or any other value to check, or none that i can see, unless they mean convert y to binary as per their example. Any Ideas on how to get this one started?
• 01-17-2012, 09:48 PM
eRaaaa
Re: Checking if a Bit is Set.
Quote:

I'm not given a number in binary
all is in binary internally :D

if you have an int = 2 --> its 0....010.
-->
Code:

```public static boolean checkBit(int x, int k) {                 return (x & 1 << k) != 0; }```
x=2, k=1 = true
Read: Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
• 01-18-2012, 04:06 PM
Hypnos
Re: Checking if a Bit is Set.
Alright, thanks!
is there a way now to be able to input which bit you'd search in, like with the Scanner Method?
so instead of just one example, i could have the prof search anywhere for himself?

Also
How would i be able to print out True or False pending on the respected outcome.
I would guess with an If loop
but i'm not sure what variable to call on in order to get the proper output.
• 01-18-2012, 04:17 PM
Tolls
Re: Checking if a Bit is Set.
You could just print out the result.
println() is overloaded with all the primitives (int, char, double...boolean).
• 01-18-2012, 05:44 PM
Hypnos
Re: Checking if a Bit is Set.
public class TEST {
public static boolean checkBit(int x, int k)
{
k = 2;
return (x & 1 << k) != 0;
}
public static void main (String[] arg)
{
if (k == 2)
if (x == 1)
System.out.println("true");
else
System.out.println("false");
else
System.out.println("false");
}
}

First prototype of my code to see if it would work...
It however does not...
Having a hard time figuring out exactly how to do this problem
• 01-18-2012, 05:51 PM
eRaaaa
Re: Checking if a Bit is Set.
WTF?!
ok delete my preferred method and try it yourself, else your teacher knows that this is not your code :)
checkBit = method
you have to call this method from your main method!
Code:

```        public static void main(String[] args) {                 int x = 12345;                 System.out.println(x+" is binary="+Integer.toBinaryString(x));                 int length = Integer.toBinaryString(x).length();                 for (int i = 0; i < length; i++) {                         System.out.println(checkBit(x, i));                 }         }         public static boolean checkBit(int x, int k) {                 return (x & 1 << k) != 0;         }```
• 01-18-2012, 05:56 PM
Tolls
Re: Checking if a Bit is Set.
You're not calling the checkBit method anywhere.
ANd that code can't compile.
• 01-18-2012, 05:58 PM
Tolls
Re: Checking if a Bit is Set.
eRaaaa, don't spoonfeed solutions like that.
It's clear from the code posted by the OP they need help to understand what they're doing...
• 01-18-2012, 06:01 PM
Hypnos
Re: Checking if a Bit is Set.
Alright, sorry for the confusion there.
So that code will print out all the values,
if i wanted to edit the code so that it just calls on one specific bit and just prints if that's true or not
i would have to edit the main string?
• 01-18-2012, 06:05 PM
eRaaaa
Re: Checking if a Bit is Set.
Are you serious? the method returns a boolean (true or false) so you have to call this method with an int = the number to check and a second int = the bit to test! read my first post ?!?!
• 01-18-2012, 09:21 PM
doWhile
Re: Checking if a Bit is Set.
Quote:

Originally Posted by eRaaaa
Are you serious? the method returns a boolean (true or false) so you have to call this method with an int = the number to check and a second int = the bit to test! read my first post ?!?!

Perhaps you should walk the original poster through the thought process rather than spoon-feeding solutions (which is academic dishonesty BTW) and expecting the cut and paste to be understood.
• 01-18-2012, 09:27 PM
eRaaaa
Re: Checking if a Bit is Set.
Quote:

Originally Posted by doWhile
Perhaps you should walk the original poster through the thought process rather than spoon-feeding solutions (which is academic dishonesty BTW) and expecting the cut and paste to be understood.

my answer was not only related to my code/solution !
Quote:

Write a static method, called boolean checkBit( int x, int k )
but if you have tips related to his question -> please :)
i will stop writing here!