Convert bit vector (array of booleans) to an integer, and integer to bit vector, in Java.

Posted by dreeves on Stack Overflow See other posts from Stack Overflow or by dreeves
Published on 2010-05-08T16:36:19Z Indexed on 2010/05/08 16:38 UTC
Read the original article Hit count: 162

Filed under:
|
|
|
|

What's the best way to unstub the following functions?

// Convert a bit-vector to an integer. 
int bitvec2int(boolean[] b)
{
    [CODE HERE]
}

// Convert an integer x to an n-element bit-vector. 
boolean[] int2bitvec(int x, int n)
{
    [CODE HERE]
}

Or is there a better way to do that sort of thing than passing boolean arrays around?

This comes up in an Android app where we need an array of 20 booleans to persist and the easiest way to do that is to write an integer or string to the key-value store.

I'll post the way we (Bee and I) wrote the above as an answer. Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about android