The fastest way to convert integer into binary byte array and vice versa
Hi,
I know there are quite a few similar questions which have been posted here, and I know how to do this in a naive way based on the conversion between decimal and binary numbers, but I wonder what is the most optimal (mainly in terms of speed) way to do it.
To reiterate, say if I have an integer i, and I want to convert i into a byte array full of 0s and 1s, and vice versa.
Many thanks.
[416 byte] By [
cyaevana] at [2007-11-26 20:32:12]

What's your best attempt?
The fastest way to convert would be not converting at all.You can manipulate the individual bytes that make an integer without converting it to an array of bytes.
> To reiterate, say if I have an integer i, and I want> to convert i into a byte array full of 0s and 1s, and> vice versa.Why would you want to do that? An int is already binary.And what would the format be? 32 bytes, of zero or one?
> I know how to do this in a naive way based on the conversion between decimal and binary numbers,
Primitive integers are normally stored in binary, not decimal.
Is your input an int, or a string of decimal digit characters, or a binary coded decimal (eg 127 bcd is represented 0x127 hex)?
> if I have an integer i, and I want to convert i into a byte array full of 0s and 1s
Is your output an array of 4 bytes, with bit patterns representing parts of of the 32 bit pattern of int, or an array of 32 bytes, each having a value of 1 or 0?
> Is your output an array of 4 bytes, with bit patterns> representing parts of of the 32 And if it is, then why not just work with an int, which is a single value with a 32-bit bit pattern representing the value of the int.
public static byte[] convert(int n) {
return new byte[] {
(byte) n,
(byte)(n >> 8),
(byte)(n >> 16),
(byte)(n >> 24),
};
}
public static int convert(byte[] v) {
return (0xff & v[0]) |
(0xff & v[1]) << 8 |
(0xff & v[2]) << 16 |
v[3] << 24;
}
Sorry guys, just realised that the question is wrong in my case since an integer is not able to solve my problem here.
Ok, let me start it again by stating what exactly I want to achieve here using some sort of conversions.
The reason I want to convert an array of bytes of binary back and forth is to reduce the memory storage needed.
Here's an example:
The program creates hundreds of objects and each of which needs to contain an array of 0s and 1s that is used for some sort of mappings (this is sth specfic to my application). The length of the array could be different from one object to another, but the lengths of these arrays are pretty long, e.g. could be as long as 2^20. So if I use an array of bytes to represent this, that's going to take a lot memory at runtime.
So what I intend to do here is to have some sort of encoding of these arrays, so they can be stored in the object using this encoding with much less memory, and they only get converted into array form when needed. Since the logic of the program is going through all these objects one by one in a loop, so the total memory at any time won't be too bad even though you may have one such huge array in place at each round of the loop.
I hope the above makes some sense, if not, I'll try explain in a bit more details. Thanks.
What do you mean "array of 0s and 1s"?For that I'd either use a bunch of ints or longs, or a java.util.BitSet.
The reason I need these arrays of 0s and 1s is because I need to do some sort of mapping like this:
a function in my program returns an integer as the index into such an array and then get the bit at that location (either 0 or 1 in this case). And that bit will be used for some other calculations. And I need to do this for every single object (which each has such an array thingy) at each turn. This is why I use an array (which has order) here. I'm not entirely sure if this is the optimal way to do it, but it looks intuitive enough to me.
Any suggestion on how I can do this in a better way is welcome. Thanks.
> The reason I need these arrays of 0s and 1s is
But what do you mean by "array of 0s and 1s"? There's no such thing in Java.
There's an int, which is 32 0s and 1s, but its not an array.
There's arrays of ints, arrays of bytes, etc. but those arrays aren't "arrays of 0s and 1s".
From what you describe, java.util.BitSet sounds like what you want.
>But what do you mean by "array of 0s and 1s"? >There's no such thing in Java.
>
>There's an int, which is 32 0s and 1s, but its not an >array.
>
>There's arrays of ints, arrays of bytes, etc. but those >arrays aren't "arrays of 0s and 1s".
>
>From what you describe, java.util.BitSet sounds like >what you want.
Sorry, what I meant by an array of 0s and 1s means an array of, for example, bytes, but for each of these bytes, the value would only be either 0 or 1.
Does this clarify what I intend to do here. Thanks.
> Sorry, what I meant by an array of 0s and 1s means an
> array of, for example, bytes, but for each of these
> bytes, the value would only be either 0 or 1.
>
> Does this clarify what I intend to do here. Thanks.
How many times do we have to say java.util.BitSet? There is no such thing as an "array of bits" and BitSet accomplishes exactly what you appear to want.
> >But what do you mean by "array of 0s and 1s"?
> >There's no such thing in Java.
> >
> >There's an int, which is 32 0s and 1s, but its not
> an >array.
> >
> >There's arrays of ints, arrays of bytes, etc. but
> those >arrays aren't "arrays of 0s and 1s".
> >
> >From what you describe, java.util.BitSet sounds like
> >what you want.
>
> Sorry, what I meant by an array of 0s and 1s means an
> array of, for example, bytes, but for each of these
> bytes, the value would only be either 0 or 1.
>
> Does this clarify what I intend to do here. Thanks.
Okay, so have you looked into BitSet, as I suggested? If that won't work for you, why not?
> Okay, so have you looked into BitSet, as I suggested?> If that won't work for you, why not?Thanks for the advice. I'll look into it.