# Thread: How to restrict my binary output to 8 bits or 4 bits ?

1. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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 .

Thanks

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

3. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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?

Originally Posted by jim829
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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

5. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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`

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

Originally Posted by jim829
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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

Integer.toBinaryString(value &0xFF);

Regards,
Jim

7. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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

Originally Posted by jim829

Integer.toBinaryString(value &0xFF);

Regards,
Jim

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

9. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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?

Originally Posted by jim829
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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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 04:37 AM.

11. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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 04:49 AM. Reason: link didn't show up ... :(

13. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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?

Originally Posted by jim829
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. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

16. Senior Member
Join Date
Nov 2011
Posts
165
Rep Power
9

## 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

Originally Posted by jim829
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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

#### Posting Permissions

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