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

Here is my code:

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:

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

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.

Regards,

Jim

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

Regards,

Jim

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:

Code:

`int a &=0b1111_1111; // Java 7`

Regards,

Jim

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

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

Code:

`Binary Equivalent at this stage:11100000`

instead of

Code:

`Binary Equivalent at this stage: 11111111111111111111111111100000`

Does that make sense?

Quote:

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:

Code:

`int a &=0b1111_1111; // Java 7`

Regards,

Jim

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

How about:

Integer.toBinaryString(value &0xFF);

Regards,

Jim

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** How about:

Integer.toBinaryString(value &0xFF);

Regards,

Jim

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

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

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

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.

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.

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

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:

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

Code:

`int left = allOnes << (j+1);`

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

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:

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

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

Code:

`Integer.toBinaryString(allOnes);`

Regards,

Jim