2's complement binary form of an int?

G

George Cherry

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
 
P

Paulus de Boska

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
 
R

Roedy Green

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
 
O

Oliver Wong

Paulus de Boska said:
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
 
R

Roedy Green

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;
 
A

Adam Warner

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.

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
 
R

Roedy Green

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' );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top