Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Numbers & Binary

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Numbers & Binary

    Hey guys. So, I have a piece of code that works:

    public static boolean[] sub(int a) {
    		assert (a >= 0 && a <= 7);
    		boolean uread = false, uwrite = false, uexecute = false;
     
    		switch (a) {
    		case 0:
    			uread = false;
    			uwrite = false;
    			uexecute = false;
    			break;
    		case 1:
    			uread = false;
    			uwrite = false;
    			uexecute = true;
    			break;
    		case 2:
    			uread = false;
    			uwrite = true;
    			uexecute = false;
    			break;
    		case 3:
    			uread = false;
    			uwrite = true;
    			uexecute = true;
    			break;
    		case 4:
    			uread = true;
    			uwrite = false;
    			uexecute = false;
    			break;
    		case 5:
    			uread = true;
    			uwrite = false;
    			uexecute = true;
    			break;
    		case 6:
    			uread = true;
    			uwrite = true;
    			uexecute = false;
    			break;
    		case 7:
    			uread = true;
    			uwrite = true;
    			uexecute = true;
    			break;
    		}
     
    		return new boolean[] { uread, uwrite, uexecute };
    	}

    I am 99 percent sure that can be done using shifts and other binary operations. My question is how?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,714
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Numbers & Binary

    Please add some comments and/or a method description to let us know what this method takes as input, what it does with the input to achieve the desired output, what the desired output is, and a description of how the code achieves the desired result.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    It takes an int, 0-7. Outputs a 3 long boolean array. The switch case pretty much explains what it does... If I rewrite the output for each input, you'll get essentially the switch case. I'll do it anyway however.

    0 false false false
    1 false false true
    2 false true false
    3 false true true
    4 true false false
    5 true false true
    6 true true false
    7 true true true

  4. #4
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    447
    Thanks
    0
    Thanked 86 Times in 71 Posts

    Default Re: Numbers & Binary

    You dont neccessarily need binary operations here. Boolean logic would be enough.
    Look at each of the booleans separately and try to find a formula which works only for that boolean.

    For example, the first boolean is true when the value of the argument is bigger then or equal to 4.
    Use the same kind of logic on the other booleans and you can narrow your code down to a few lines of code.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    If the max value is seven, that means there are 3 bits of interest.
    Use a bit mask to test those three bits and set the corresponding slot in the boolean array true or false depending if the bit in that slot is on.

    loop number of bits times
    if bit on set true
    if bit off set false
    increment array index
    shift bit by 1
    end loop

    Some examples (1 & 3 == 1) (4 & 3 == 0)
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    Hmm... I'm fairly new to this kind of logic... I haven't worked with this kind of thing very much. I kind of understood the pseudocode but really don't know how to write it... could you go more into detail please? I really need to learn this stuff in detail...

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    I assume you understand what the AND operator does: 1 & 1 == 1 // 1 & 0 = 0
    Use the AND operator with what I call a "bit mask" (an int with one of its bits set to 1) to test if the corresponding bit in the value being tested is on or not.
    EG 0x04 would test bit 5 with bits (0123 4567) in a byte
    Shift the bit in the bit mask to the next position to test that position.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    Ok, that makes it a little clearer... but I still don't understand how it gives me my 3 booleans from the one int...
    I don't want to ask for code and wont (not that I expect it would be given anyway) but I am having a bit of a struggle understanding...

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    how it gives me my 3 booleans from the one int...
    There are 32 bits in an int, with a loop all 32 bits could be tested (AND and shift) and a boolean value stored in a boolean array with 32 slots.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    I think something just clicked in my brain
    I was forgetting that the pattern I need is binary... so .. essentially I just create a int to binary (in the form of a boolean array, only 3 bits) converter...
    Lightbulb moment.

    EDIT:
    I am .. close..
    public static boolean[] sub(int a) {
    		assert (a >= 0 && a <= 7);
    		boolean[] arr = new boolean[3];
    		int idx = 0;
    		for (int i = 0; i <= 7; i++) {
    			if ((a & (1 << i)) == 1) {
    				arr[idx] = true;
    				idx++;
    			}
    		}
     
    		return arr;
    	}
    Doesn't quite work...
    Last edited by sci4me; July 8th, 2014 at 02:08 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    Doesn't quite work..
    Please explain.
    Use the Arrays class's toString method to show the array's contents:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    How many bits are to be tested?
    How many times will the for statement loop?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    Oh, derp. So, loop 3 times... right? That's giving me the same results.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    That's giving me the same results.
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    This is what it's giving me now:

    0 [false, false, false]
    1 [true, false, false]
    2 [false, false, false]
    3 [true, false, false]
    4 [false, false, false]
    5 [true, false, false]
    6 [false, false, false]
    7 [true, false, false]

    Wait, setting it to != 0 instead of == 1 makes it work? Heh, works for me!
    Last edited by sci4me; July 8th, 2014 at 02:33 PM.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    Looks like only the odd numbers get a true for the low bit.

    Do some debugging: print out the value of the bit mask that is used to see why none of the other positions are tested.

    And print out the results of the AND to see what that is.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    This is what I have:
    boolean[] array = new boolean[3];
    for(int i=0; i<3; i++)
    {
        array[i] = (a & (1 << i)) != 0;
    }
    works beautifully. I figured this out by printing the result of the and... it was not just 1's and 0's so I figured that != 0 would do the trick. It did and does

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    Great. Glad you got it working. I might have mislead in my explanation.
    Not zero is a good compare for a single bit. But if more than one bit is being tested for, then the compare should be for equal to the "bit mask":
    if( (someInt & bitMask) == bitMask)
    For example. The number to test is on the left, bitmask = 3:
    7 & 3 = 3 // here both bits are on
    6 & 3 = 2 // here only one bit is on
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    sci4me (July 9th, 2014)

  19. #18
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Numbers & Binary

    Oh I see... then.. how do you know which two bits are on... I mean, yes the 6 tells that but.. eh..

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,076
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Numbers & Binary

    how do you know which two bits are on
    You know because you have built the "bit mask". The mask: 0x03 tests the 2 rightmost bits.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. numbers to words using do-while & if-else & JOptionPane
    By mikkko in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 15th, 2011, 05:28 AM