... 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();
}
}
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
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
> 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.
The handy one-liner:static String format(int i) {
return Long.toBinaryString((1L << 32) | (i & (-1L >>> 32))).substring(1);
}
> The handy one-liner:> static String format(int i)
> {
> return Long.toBinaryString((1L << 32) | (i & (-1L >>> 32))).substring(1);
> }
Hacker ...
kind regards,
Jos ;-)