I have a long variable in java and am converting it to a binary string, like
long var = 24;
Long.toBinaryString(val);
Now this prints only 7 bits, but I need to display all the 64 bits, i.e. all the leading zeros also, how can I achieve this?
The reason is I need to iterate through each bit and perform an operation according to the status, is there a better way to do it?
If you want to iterate through the bits, you might be better off testing each bit without converting it to a string:
But, if you truly want a binary string, this is the easiest way to left-pad it with zeros: