Results 1 to 17 of 17
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default How to restrict my binary output to 8 bits or 4 bits ?

    Here is my code:

    Java Code:
    public class Bits{ 
       public static void main(String args[]){
    	  
    	   
    	   int i = 2 , j = 4;
    	   
    	
    	   
    	   int allOnes = ~0;
    	   
    	   int left = allOnes << (j+1);
    	   
    	   System.out.println("Binary Equivalent at this stage: " +Integer.toBinaryString(left));
    	   
    	  }
    }
    The following is the output I'm getting:

    Java Code:
    Binary Equivalent at this stage: 11111111111111111111111111100000

    How can I restrict it to only 8 bits from the right hand side. I mean 11100000 .


    Please explain.

    Thanks

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Trying ANDing it with 0xFF before printing it out. That should make the bits from 31 to 8 all zeroes, the least significant bit being bit #0.

    Java Code:
    int a &= 0xFF;
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Yeah, but can't we just limit it to 8 bits only for the sake of clarity?


    Quote Originally Posted by jim829 View Post
    Trying ANDing it with 0xFF before printing it out. That should make the bits from 31 to 8 all zeroes, the least significant bit being bit #0.

    Java Code:
    int a &= 0xFF;
    Regards,
    Jim

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Not certain what you mean. Unless you want to use a byte value which holds only 8 bits (range = -128 to 127). Or did you mean
    something like this:
    Java Code:
    int a &=0b1111_1111;   // Java 7
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Sorry for the confusion. I would like the following output:

    Java Code:
    Binary Equivalent at this stage:11100000
    instead of

    Java Code:
    Binary Equivalent at this stage: 11111111111111111111111111100000
    Does that make sense?

    Quote Originally Posted by jim829 View Post
    Not certain what you mean. Unless you want to use a byte value which holds only 8 bits (range = -128 to 127). Or did you mean
    something like this:
    Java Code:
    int a &=0b1111_1111;   // Java 7
    Regards,
    Jim

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    How about:

    Integer.toBinaryString(value &0xFF);

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

  7. #7
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Cool, that worked ! Thanks a lot ! Could you explain more about how come the long list of bits disappeared after ANDing the value with 0xFF .

    Thanks

    Quote Originally Posted by jim829 View Post
    How about:

    Integer.toBinaryString(value &0xFF);

    Regards,
    Jim

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    It has to do with bit wise operations. Think of each bit as either true(1) or false(0).

    1 and 1 = 1
    1 and 0 = 0
    0 and 1 = 0
    0 and 0 = 0

    so essentially what you did was

    111111111111111111111111100000 & 00000000000000000000000011111111;

    The right set of bits is called a mask. Everywhere a 0 bit exists in the mask, the corresponding bit is turned off. Every place a 1 bit is in the mask, the corresponding bit is left as it is.

    The bit wise Or (|) operation is just the opposite.

    1 or 1 = 1
    1 or 0 = 1
    0 or 1 = 1
    0 or 0 = 0

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

  9. #9
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Thanks for the detailed explanation. I understood the basic AND and OR operations. I still have the following two questions:

    Considering the following operation:

    111111111111111111111111100000 & 00000000000000000000000011111111;


    Why the output has been reduced to 11100000, that is only 8 bits? I know that I was asking for 8 bits solution but I didn't understand why it reduced to 8 bits after masking with 0xFF.

    2) Shouldn't the output be "00000000000000000000000011100000" instead of just "11100000" ? I'm wondering how did it happen?

    Thanks in advance !

    Quote Originally Posted by jim829 View Post
    It has to do with bit wise operations. Think of each bit as either true(1) or false(0).

    1 and 1 = 1
    1 and 0 = 0
    0 and 1 = 0
    0 and 0 = 0

    so essentially what you did was

    111111111111111111111111100000 & 00000000000000000000000011111111;

    The right set of bits is called a mask. Everywhere a 0 bit exists in the mask, the corresponding bit is turned off. Every place a 1 bit is in the mask, the corresponding bit is left as it is.

    The bit wise Or (|) operation is just the opposite.

    1 or 1 = 1
    1 or 0 = 1
    0 or 1 = 1
    0 or 0 = 0

    Regards,
    Jim

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Because the high order bits of 0 are not adding anything to the value so the string operation ignores them. I guess another way of saying it is the output of the string conversion only prints the bits starting with the most significant, non-zero bit.

    Regards,
    Jim
    Last edited by jim829; 03-25-2013 at 05:37 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?



    The API docs are worth bookmarking if you haven't done so already.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Integer (Java Platform SE 7 )

    The API docs are worth bookmarking if you haven't done so already.
    Last edited by pbrockway2; 03-25-2013 at 05:49 AM. Reason: link didn't show up ... :(

  13. #13
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Thanks for the answer. I really appreciate that. I have few more questions:

    1) Could you tell me any source which says that the "string conversion only prints the bits starting with the most significant, non-zero bit." I would like to read more about it.


    2) As far as the mask "0xFF" is concerned, I understood that 0x stands for hexadecimal, then don't you think that we are masking a hexadecimal value with an integer value and still it doesn't throws any errors?




    Quote Originally Posted by jim829 View Post
    Because the high order bits of 0 are not adding anything to the value so the string operation ignores them. I guess another way of saying it is the output of the string conversion only prints the bits starting with the most significant, non-zero bit.

    Regards,
    Jim

  14. #14
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Also, I have one more Question which is totally different with the above one:

    Java Code:
    public static void main(String args[]){
           
            
           int i = 2 , j = 4;
            
         
            
           int allOnes = ~0; // will equal sequence of all 1s
            
           int left = allOnes << (j+1); 
            
           System.out.println("Binary Equivalent at this stage: " +Integer.toBinaryString(left));
            
          }
    }
    Since I didn't understand the following line:
    Java Code:
    int allOnes = ~0; // will equal sequence of all 1s

    When I tried to output the value of "allOnes" then I got "-1" as my output.


    I'm having hard time understanding the very next line which is as follows:

    Java Code:
    int left = allOnes << (j+1);

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Negative numbers are stored in two's complement form. If the high-order bit is a one, the result will always be negative for a signed number. The << operator means shift left n bits when n in this case is j+1.

    This is not difficult stuff but would take a while to explain. So I suggest you do a web search on bit operations, truth tables and two's complement math. They are all basic computer science topics. And when you have mastered that, you can check out the IEEE 754 standard to see how floating points numbers are stored.

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

  16. #16
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    Thanks for your answer. I tried a simple program for understanding the "~0" operation but still it's printing -1. Shouldn't it be sequence of 1s ?
    Here is the code I tried:

    Java Code:
    public class Bits {
    
    	
    	public static void main(String[] args) {
    		
    		
    		int allOnes = ~0;
    		
    		System.out.println(allOnes);
    		
    	}
    
    }
    Out Put is : -1


    Quote Originally Posted by jim829 View Post
    Negative numbers are stored in two's complement form. If the high-order bit is a one, the result will always be negative for a signed number. The << operator means shift left n bits when n in this case is j+1.

    This is not difficult stuff but would take a while to explain. So I suggest you do a web search on bit operations, truth tables and two's complement math. They are all basic computer science topics. And when you have mastered that, you can check out the IEEE 754 standard to see how floating points numbers are stored.

    Regards,
    Jim

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

    Default Re: How to restrict my binary output to 8 bits or 4 bits ?

    No because even though internally it is a sequence of ones, it is being printed as a signed integer. So the value is -1. Try printing it using

    Java Code:
    Integer.toBinaryString(allOnes);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Convert Int (last 8 bits) to Byte
    By gonzalioz in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2011, 09:38 PM
  2. hiding bits in image
    By stegano in forum New To Java
    Replies: 1
    Last Post: 04-07-2011, 04:46 PM
  3. Multiply, or shift bits?
    By plm-pusik in forum New To Java
    Replies: 4
    Last Post: 03-15-2011, 11:08 PM
  4. Help with random bits array
    By AnimeKitty in forum New To Java
    Replies: 11
    Last Post: 07-30-2010, 05:37 AM
  5. Image to Binary bits
    By Deva in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 05:49 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
  •