Results 1 to 12 of 12
  1. #1
    Hypnos is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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. #3
    Hypnos is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default 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. #5
    Hypnos is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Checking if a Bit is Set.

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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default 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. #9
    Hypnos is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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. #10
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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. #11
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Checking if a Bit is Set.

    Quote Originally Posted by eRaaaa View Post
    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. #12
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Checking if a Bit is Set.

    Quote Originally Posted by doWhile View 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.
    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!

Similar Threads

  1. Checking if a variable is set in jsp
    By saurabh1989 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 02-02-2011, 03:14 PM
  2. how to do checking value
    By madhuks in forum Java Servlet
    Replies: 3
    Last Post: 07-21-2010, 10:35 AM
  3. Checking udppackets in xp
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 05-25-2010, 04:48 AM
  4. Checking if a file name already has .txt in it
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:45 PM

Posting Permissions

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