Converting long to binary in Java gives 31 bits? -
i understand unix time should 32 bit (4 byte) value. calculate current unix time, i'm using following:
long time = (system.currenttimemillis())/1000;
this works fine - same value when on unix command shell: "date +%s".
i want represent in binary, though. tried this:
system.out.println(long.tobinarystring(time));
however, 31 bits, not 32 have expected. missing?
thanks!
tim
the leading zeros aren't being displayed. try this:
long l = -1; system.out.println(long.tobinarystring(l));
or:
long l = long.min_value; system.out.println(long.tobinarystring(l));
or:
long l = -2112; system.out.println(long.tobinarystring(l));
you should see 64 bits. (long in java 64 bits, not 32)
i hope helps.
edit:
if want string representation of 32 bits leading zeros, of number of things. 1 is:
int = 42; string result = string.format("%32s", integer.tobinarystring(i)).replace(' ', '0'); system.out.println(result);
another is:
int = 42; string result = string.format("%032d", integer.parseint(integer.tobinarystring(i))); system.out.println(result);
Comments
Post a Comment