How do I display the bit pattern of a negative
int (i.e., the TWO's COMPLEMENT binary form)?
Integer.toString(n, 2) doesn't print out the
2's complement form of a negative int. I
don't want to concoct it for my students:
I want to display what's there. I wonder why
Integer.toString(n, 2) changes the form of
negative numbers.
George
Paulus de Boska - 02 Dec 2005 22:59 GMT
This is what I could come up with right now, haven't figured out a
straightforward way to display leading zeroes,though.
System.out.println(Integer.toBinaryString(-1));
System.out.println(Integer.toBinaryString(-2));
System.out.println(Integer.toBinaryString(-3));
System.out.println(Integer.toBinaryString(Integer.MIN_VALUE));
System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));
System.out.println(Integer.toBinaryString(Integer.MAX_VALUE-1));
System.out.println(Integer.toBinaryString(1));
Kind regards,
Paul Hamaker
SEMM
http://javalessons.com
Oliver Wong - 02 Dec 2005 23:25 GMT
> This is what I could come up with right now, haven't figured out a
> straightforward way to display leading zeroes,though.
You would probably want to use java.util.Formatter. For binary, you
could use the NumberFormatter because the alphabet used to represent binary
numbers is a subset of the alphabet used to represent decimal numbers, but
it wouldn't work well with hexadecimal for example.
You can provide a "minimum width" for the output string. I haven't
examined, but perhaps you can see that the padding character to enforce this
minimum width be '0'. If not, then it is probably hardcoded to be ' ', and
then you would have to do the extra step of converting all whitespace to
'0'.
- Oliver
Roedy Green - 02 Dec 2005 23:38 GMT
>System.out.println(Integer.toBinaryString(1));
something like this
// unsigned, without lead 0
String s = Integer.toBinaryString( v );
int LeadZeros = 32 - s.length();
// 32 zeros
return "00000000000000000000000000000000".substring( 0, leadZeros ) +
s;

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 02 Dec 2005 23:07 GMT
On Fri, 2 Dec 2005 17:18:10 -0500, "George Cherry"
<GWCherryHatesGreenEggsAndSpam@alum.mit.edu> wrote, quoted or
indirectly quoted someone who said :
>Integer.toString(n, 2) doesn't print out the
>2's complement form of a negative int. I
>don't want to concoct it for my students:
>I want to display what's there. I wonder why
>Integer.toString(n, 2) changes the form of
>negative numbers.
Quoting from http://mindprod.com/jgloss/binary.html#DISPLAY
// Displaying an int in binary format
int v = 5;
// signed
System.out.println( Integer.toString( v, 2 )); // 101
System.out.println( Integer.toString( -v, 2 )); // -101
// unsigned
System.out.println( Integer.toBinaryString( v )); // 101
System.out.println( Integer.toBinaryString( -v ));
// 11111111111111111111111111111011

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Adam Warner - 03 Dec 2005 09:19 GMT
> How do I display the bit pattern of a negative
> int (i.e., the TWO's COMPLEMENT binary form)?
[quoted text clipped - 5 lines]
> Integer.toString(n, 2) changes the form of
> negative numbers.
See below. Simply mask the top bit, shift it right 31 times and add it to
ASCII '0'. Shift the input left 1 and repeat another 31 times. Note that
the right shift is an unsigned shift.
public class Bits {
public static String intBits(int i) {
StringBuilder sb=new StringBuilder();
for (int j=0; j<32; ++j) {
sb.append('0'+(i & 0x80000000)>>>31);
i<<=1;
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(intBits(123));
System.out.println(intBits(Integer.MAX_VALUE));
System.out.println(intBits(-123));
System.out.println(intBits(-1));
}
}
00000000000000000000000001111011
01111111111111111111111111111111
11111111111111111111111110000101
11111111111111111111111111111111
Regards,
Adam
Roedy Green - 03 Dec 2005 14:54 GMT
On Sat, 03 Dec 2005 22:19:34 +1300, Adam Warner
<usenet@consulting.net.nz> wrote, quoted or indirectly quoted someone
who said :
> for (int j=0; j<32; ++j) {
> sb.append('0'+(i & 0x80000000)>>>31);
> i<<=1;
I think this would be simpler if you did not left shift then right
shift.
for int bit=31; bit< 0; bit-- )
{
sb.append(( ( i>>>bit) & 1) + '0' );
}

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.