How can I convert a integer to its bit representation. I want to take an integer and return a vector that has contains 1\'s and 0\'s of the integer\'s bit representation.
Here is a version that works with negative numbers:
string get_bits(unsigned int x) { string ret; for (unsigned int mask=0x80000000; mask; mask>>=1) { ret += (x & mask) ? "1" : "0"; } return ret; }
The string can, of course, be replaced by a vector or indexed for bit values.