Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cycling through bits java

Options
  • 11-03-2003 3:03pm
    #1
    Posts: 0


    Hey all,

    Can anyone tell me how to cycle through each bit in a number in Java and see if each bit is 1 or 0?

    I have this code

    for(int i = 0; i < 8; i++)
    {
    if( (test & temp) != 0){
    System.out.print("yes");
    }


    temp = (MASK << i);
    }

    it won't work though...am I doing something wrong in my logic?
    Thanks for any help


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    What is test? Why is temp being set after the conditional check?


  • Registered Users Posts: 1,186 ✭✭✭davej


    <edit>if i'm not answering your question, it's because it's not very clear</edit>

    RTFM: The static method Integer.toBinaryString(int)

    STFW:

    String binaryString = Integer.toBinaryString(42);
    int endIndex = binaryString.length() % 8;
    binaryString = "0000000".substring(0, endIndex) + binaryString;
    System.out.println(binaryString);


    davej


Advertisement