Thread: Checking if a Bit is Set.

1. Member
Join Date
Jan 2012
Posts
6
Rep Power
0

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?

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8

Re: Checking if a Bit is Set.

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

if you have an int = 2 --> its 0....010.
-->
Java 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)

3. Member
Join Date
Jan 2012
Posts
6
Rep Power
0

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.

4. Moderator
Join Date
Apr 2009
Posts
13,212
Rep Power
23

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).

5. Member
Join Date
Jan 2012
Posts
6
Rep Power
0

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

6. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8

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!
Java 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;
}```

7. Moderator
Join Date
Apr 2009
Posts
13,212
Rep Power
23

Re: Checking if a Bit is Set.

You're not calling the checkBit method anywhere.
ANd that code can't compile.

8. Moderator
Join Date
Apr 2009
Posts
13,212
Rep Power
23

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

9. Member
Join Date
Jan 2012
Posts
6
Rep Power
0

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?

10. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8

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 ?!?!

11. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9

Re: Checking if a Bit is Set.

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.

12. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8

Re: Checking if a Bit is Set.

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 !
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!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•