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 12 of 12

Thread: Help me understand this code?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help me understand this code?

    Hi,

    This is my first post here. I have have some code that prints out the possible 2-set combinations of numbers in a list, with some numbers on the right side and the rest of the numbers on the left side. However, I have been studying it for quite a while and I do not understand how it is doing what it is doing. Specifically, I do not understand the function of the bitwise right-shift and bitwise AND operators in the for loops. (In fact, this is the first time I have seen bitwise operators used in programming, so please forgive my ignorance).

    I see how the bitwise left-shift operator is functioning to make the outer loop execute a certain number of times (which would determine how many combinations of numbers there would be, which would in turn be determined by the size of the original list, i.e. a list of length five would produce 2^5 - 2 combinations, because we have to have at least one number on each side of the list), but I do not understand how the right-shift and AND segment is working. It seems that it must be what determines which new list each number is in, but how? I do not understand what is happening here and would really appreciate any guidance you could give.

    Thanks.

    public class SomeNumbers
    {
     
    	public static void main(String[] args)
    	{
    		int[] list = {1, 7, 6, 8, 9};
     
     
    		for (int i = 1; i < (1 << list.length) - 1; i++) 
    	    {
    			System.out.print("(");
    			for (int j = 0; j < list.length; j++) 
    	        {
    	            if (((i >> j) & 1) == 1) {
    	                System.out.print(list[j] + " ");
    	            }
    	        }
     
    	        System.out.print(") (");
    	        for (int j = 0; j < list.length; j++) 
    	        {
    	            if (((i >> j) & 1) == 0) 
    	            {
    	                System.out.print(list[j] + " ");
     
    	            }
    	        }
    	        System.out.println(")");
    	    }
     
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Help me understand this code?

    The AND operator is used to test if a bit is set. for example:
    1010 AND 0001 is 0 meaning the rightmost bit is not set
    1001 AND 0001 is 1 meaning the rightmost bit is set
    If you have an int value (32 bits) and want to see if the 4th bit is a 1,
    AND it with 00010000000000000000000000000 and test if the result is 0 meaning it is not set

    The shift moves the bits by the number specified: 100 >> 2 gives 001

    To see what the code is doing, add some println() statements and use one of the Integer class's methods to format the int values.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Farkuldi (November 12th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand this code?

    Thank you. I am trying to make sense of it with some println() statements. I am using Integer.toBinaryString(int i), but it does not return the leading zeros. Doesn't Java have a way to show all the digits of a binary string, or would I have to write my own method if I wanted that?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Help me understand this code?

    I don't know of any display methods that show leading 0s.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand this code?

    Thanks again. I don't seem to be getting anywhere, though. I am trying to see what the code does by putting the println() statements in as you suggested, but when I write
    System.out.println("binary " + Integer.toBinaryString(1 >> 2));
    ,

    for example, it just says 0. I am still not able to see what is happening. What Integer methods should I be using if not toBinaryString(int i)?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Help me understand this code?

    A 1 shifted right would go off the end. Try shifting 0x08 to the right 1, 2 or 3 places.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand this code?

    Quote Originally Posted by Norm View Post
    A 1 shifted right would go off the end. Try shifting 0x08 to the right 1, 2 or 3 places.
    Hi,

    I am sorry I did not respond on Sunday.

    I do see the way 1000 shifted right 1, 2, and 3 places becomes 0100, 0010, and 0001, thank you. That makes sense. I guess what I still do not understand is what it is doing in the context of this code. Specifically, this segment:

                    for (int j = 0; j < list.length; j++) 
    	        {
    	            if (((i >> j) & 1) == 1) {
    	                System.out.print(list[j] + " ");
    	            }
    	        }

    This code is in another, outer loop, so let's say i = 1. On the first trip through the j loop, j = 0. So in the if clause, we shift i (which is 1) right by 0 places, which is still 1, and then apply the rightmost bit mask. So that should produce a 1, and we process the statement in the if clause.

    However, on the next trip through the j loop, we have i = 1 and j = 1. So, what is the meaning of 1 >> 1? Would it be 0.1? Can you just have a decimal binary number, or do we need to take into account the twos complement representation or some other format? What happens now?

    I appreciate your patience.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Help me understand this code?

    1 >> 1
    Print it out to see what it is.

    (results & 1) == 1)
    That is the test to see if results has a 1 in the right most bit.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand this code?

    Okay, it says it is zero. But why is it zero? I do not mean to belabor the point, but if 1 is shifted right one place, I do not see how that would produce zero unless there are more rules than have been stated here. Does everything to the right of the ones place just "fall off?" I would just like to understand what is happening.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Help me understand this code?

    Does everything to the right of the ones place just "fall off?"
    Yes, unless the shift does a wrap which moves the bit to the high end.

    Think of shift as int division: 1/2 is 0
    2(binary 10) shifted right 1 is 1 and 2/2 is 1
    If you don't understand my answer, don't ignore it, ask a question.

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

    Farkuldi (November 12th, 2013)

  13. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help me understand this code?

    Thank you. I understand now.

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Help me understand this code?

    Your welcome.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. whats wrong with code, please help me understand thanks
    By guled in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 22nd, 2013, 12:45 PM
  2. Hi,Can any one help me to understand this code.Am a beginner.
    By Jawad24 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 23rd, 2013, 07:47 AM
  3. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  4. [SOLVED] Gotten easy code by proffessor that I don't understand, do you?
    By matitorn in forum Java Theory & Questions
    Replies: 7
    Last Post: October 9th, 2012, 01:30 PM
  5. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 12:06 PM