Results 1 to 4 of 4
  1. #1
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Bisewise and Binary, Help please

    Hello, so I need to use the use bisewise operators and have in the end 1's and 2's as a result but as a binary, now I had tested so I know that the result it's 1's and 2's, however when I use .toBinaryString it doest output binary numbers, I also tested with other numbers and it does work, but not for 1's and 2's, any idea why? I have below the output of the program, thank you
    Java Code:
    public class TestBitwise {
    
    	public static void main(String[] args) {
    		
    		int i = 1;
    		Integer y = 1;
    		int x = y & i;
    		System.out.println (Integer.toBinaryString(x));
    		//byte byte1 = 16;
    		//System.out.println(x);
    		
    		int i1 = 1;
    		Integer y1 = 1;
    		int x1 = y1 << i1;
    		System.out.println (Integer.toBinaryString(x1));
    		//System.out.println(x1);
    		
    		int i2 = 1;
    		Integer y2 = 0;
    		int x2 = y2 ^ i2;
    		System.out.println (Integer.toBinaryString(x2));
    		//System.out.println(x2);
    		
    		int i3 = 2;
    		Integer y3 = 2;
    		int x3 = y3 | i3;
    		System.out.println (Integer.toBinaryString(x3));
    		//System.out.println(x3);
    		
    		int i4 = 0;
    		Integer y4 = 1;
    		int x4 = y4 >> i4;
    		System.out.println (Integer.toBinaryString(x4));
    		//System.out.println(x4);
    		
    		int i5 = 1;
    		Integer y5 = 1;
    		int x5 = y5 << i5;
    		System.out.println (Integer.toBinaryString(x5));
    		//System.out.println(x5);
    	}
    
    }
    output of the program is not binary
    1
    10
    1
    10
    1
    10

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Bisewise and Binary, Help please

    I assume you mean "bitwise" not "bisewise" ?

    That output is binary and is the correct answers for each operation you are performing. What are you expecting the output to be?

    110 = 12
    210 = 102

    1.
    1 and 1 = 1
    00000001
    00000001 AND
    00000001

    2.
    1 << 1 = 210 = 102
    00000001
    00000001 <<
    00000010

    3.
    1 xor 0 = 1
    00000001
    00000000 XOR
    00000001

    4.
    210 or 210 = 102 or 102 = 210 = 102
    00000010
    00000010 OR
    00000010

    5.
    1 >> 0 = 1
    00000001
    00000000 >>
    00000001
    Last edited by AndrewM16921; 09-21-2013 at 07:20 PM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Bisewise and Binary, Help please

    Looks like binary to me. Do you understand binary?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Re: Bisewise and Binary, Help please

    LOL you are right, I was using a website to check my answer and I guess I was looking at the wrong thing, but thank you for let me know that my code is correct ;)

Similar Threads

  1. Replies: 2
    Last Post: 02-22-2013, 06:10 PM
  2. Replies: 1
    Last Post: 11-01-2011, 05:35 PM
  3. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  4. binary
    By Engineer in forum New To Java
    Replies: 22
    Last Post: 12-03-2010, 09:56 AM

Tags for this Thread

Posting Permissions

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