convert integer to 8 bits
-
hi all.. i want to convert an integer number into 8 bits .. for example : if the Integer number is 5 , i want it to be 00000101. and i need the zero's in my output.. i'll be very thankful if any one have an idea ..
-
hi all.. i want to convert an integer number into 8 bits .. for example : if the Integer number is 5 , i want it to be 00000101. and i need the zero's in my output.. i'll be very thankful if any one have an idea ..
And what exactly are you having trouble with? Couldn't you just do something like:
boolean[] b = new boolean[8];
for (int i = 0; i < 8; i++)
{
b[7 - i] = (x & 1) == 1;
x >>= 1;
}warning: untested Or something that looks just like it. Perhaps an array of ints like this:
int[] b = new int[8];
for (int i = 0; i < 8; i++)
{
b[7 - i] = x & 1;
x >>= 1;
}Or perhaps not
7 - i
but justi