printing integer in a 32 bit format

Hi,How do you get a number to print out in a 32 bit format because using System.out.println(my number) is simply yielding the integer value?Many thanks, Ron
[177 byte] By [cakea] at [2007-11-26 19:50:54]
# 1
> Hi,> > How do you get a number to print out in a 32 bit> format because using System.out.println(my number) is> simply yielding the integer value?What is 32 bit format? You mean as a binary number?
tjacobs01a at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes, sorry, I am looking for it to come out like this:00001000 00000000 01001011 00100101:) Ron
cakea at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 3
Use: Integer.toBinaryString(num)You will have to put in spaces yourself, but that is easy.
DrLaszloJamfa at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 4
Have a look at the Integer.toBinaryString(int i) static method.kind regards,Jos
JosAHa at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 5

... You will also have to pad initial zeroes.

Demo:

public class BinaryExample {

public static void main(String[] args) {

for(int n=0; n<0xFFFFFFF; n += 0xA1CAFE) {

System.out.println(format(n));

}

}

public static String format(int n) {

String s = Integer.toBinaryString(n);

StringBuilder b = new StringBuilder();

//initial 0s

int pad = 32 - s.length();

for(int i=0; i<pad; ++i) {

if (i > 0 && i % 8 == 0)

b.append(' ');

b.append('0');

}

for(int i=0; i<s.length(); ++i) {

if ((i+pad) > 0 && (i+pad) % 8 == 0)

b.append(' ');

b.append(s.charAt(i));

}

return b.toString();

}

}

DrLaszloJamfa at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 6

Hi, this is what I am creating:

int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 };

int n = 0, suit = 0x8000;

int t = 0;

for ( i = 0; i < 4; i++, suit >>= 1 )

for (int j = 0; j < 13; j++, n++ )

t = primes[j] | (j << 8) | suit | (1 << (16+j));

But using System.out.println(Integer.toBinaryString(t)); doesn't yield the result I expect, it should look something like this:

00001000 00000000 01001011 00100101(King of Diamonds)

00000000 00001000 00010011 00000111(Five of Spades)

00000010 00000000 10001001 00011101(Jack of Clubs)

Ignore the (King of Diamonts) etc. I just put those there for myself as reference...

Did i misinterpret the call?

Ron

cakea at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 7
What result are you getting instead?
jverda at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 8
Then perhaps your expectation is wrong. I wouldn't be at all surprised to find that bit-twiddling code doesn't do what you expect. But we don't know what the output is from that, anyway, so it's hard to comment on it.
DrClapa at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 9

I am getting this:

11000000000000010

101000000100000011

1001000001000000101

10001000001100000111

100001000010000001011

1000001000010100001101

10000001000011000010001

100000001000011100010011

1000000001000100000010111

10000000001000100100011101

100000000001000101000011111

1000000000001000101100100101

10000000000001000110000101001

10100000000000010

100100000100000011

1000100001000000101

10000100001100000111

100000100010000001011

1000000100010100001101

10000000100011000010001

100000000100011100010011

1000000000100100000010111

10000000000100100100011101

100000000000100101000011111

1000000000000100101100100101

10000000000000100110000101001

10010000000000010

100010000100000011

1000010001000000101

10000010001100000111

100000010010000001011

1000000010010100001101

10000000010011000010001

100000000010011100010011

1000000000010100000010111

10000000000010100100011101

100000000000010101000011111

1000000000000010101100100101

10000000000000010110000101001

10001000000000010

100001000100000011

1000001001000000101

10000001001100000111

100000001010000001011

1000000001010100001101

10000000001011000010001

100000000001011100010011

1000000000001100000010111

10000000000001100100011101

100000000000001101000011111

1000000000000001101100100101

10000000000000001110000101001

lol

cakea at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 10
To the leftTo the leftyou need to pad 0's to the left.See my demo, above.
DrLaszloJamfa at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 11
Dr, thanks that now works perfectly and the numbers are expected :) Ok, I do have one more question and this is where I completely show my ignorance about everything Java, why are the lead zero's deleted?I had to ask, Ron
cakea at 2007-7-9 22:40:33 > top of Java-index,Java Essentials,Java Programming...
# 12

> Dr, thanks that now works perfectly and the numbers

> are expected :) Ok, I do have one more question and

> this is where I completely show my ignorance about

> everything Java, why are the lead zero's deleted?

They're not deleted. They're just not added in the first place.

Ints can go up to about 2,000,000,000. But if you say System.out.println(7), it doesn't print 0000000007. It just prints 7. Same thing when printing in base-2 instead of base-10. It just doesn't add the leading zeros, so 7 is 111 instead of 00000000000000000000000000000111.

jverda at 2007-7-9 22:40:34 > top of Java-index,Java Essentials,Java Programming...
# 13

The handy one-liner:static String format(int i) {

return Long.toBinaryString((1L << 32) | (i & (-1L >>> 32))).substring(1);

}

jsalonena at 2007-7-9 22:40:34 > top of Java-index,Java Essentials,Java Programming...
# 14

> The handy one-liner:> static String format(int i)

> {

> return Long.toBinaryString((1L << 32) | (i & (-1L >>> 32))).substring(1);

> }

Hacker ...

kind regards,

Jos ;-)

JosAHa at 2007-7-9 22:40:34 > top of Java-index,Java Essentials,Java Programming...