For example, 6 => [1,1,0]
            Asked
            
        
        
            Active
            
        
            Viewed 1,694 times
        
    1
            
            
        - 
                    3This isn't clear. An int *is* bit-addressable. Do you mean "print out" the bit pattern? – egrunin Dec 07 '10 at 17:42
- 
                    2Can you be more specific about what you mean by "bit representation?" – nmichaels Dec 07 '10 at 17:43
- 
                    1I think he means the binary representation. – Jacob Relkin Dec 07 '10 at 17:43
- 
                    yes. corrected the title – tomermes Dec 07 '10 at 17:43
- 
                    Is it homework? C already uses binary representation. Do you mean *how to get to it?* – ruslik Dec 07 '10 at 17:44
- 
                    @Jacob: Yes, obviously, but what does that mean? The computer only sees the binary representation. Is this for printing, or does he want to address the bits like in a bit field, or what? – nmichaels Dec 07 '10 at 17:45
- 
                    And what you want to get for `-6`? Or for `(6<<24)` you want to get the same as for `6`? – khachik Dec 07 '10 at 17:47
- 
                    Take a look at [Is there a printf converter to print in binary format?](http://stackoverflow.com/q/111928/448455). – Jaime Soto Dec 07 '10 at 19:30
4 Answers
2
            To read bits you can use
char get_bit(unsigned int n, int bit_num){
    if (bit_num < 0 || bit_num >= sizeof(int) * CHAR_BIT)
        return -1;
    return (n >> bit_num) & 1;
};
Its main problem is that it's not fast, but OP didn't even specified if he wanted numers or digits.
 
    
    
        ruslik
        
- 14,714
- 1
- 39
- 40
- 
                    I like this answer: nice and simple, and probably even optimizes into a native bit test instruction. However, a few little nitpicks: 1.) `char` is unsigned on some architectures, so returning `-1` would likely yield `255` on those architectures, 2.) Returning a `char` usually isn't much better than returning an `int` due to C's tendency to promote things, and 3.) If I were me, I'd probably just `assert(bit_num >= 0 && bit_num < sizeof(unsigned int) * CHAR_BIT);` rather than having to document the "garbage out" when there is "garbage in". – Joey Adams Dec 07 '10 at 18:08
- 
                    @Joey: well, it's a sample for a programmer that will use and refractor the code according to his/her needs, and not to be put into library :) – ruslik Dec 07 '10 at 18:14
2
            
            
        unsigned x = number;
char buf[sizeof(int)*CHAR_BIT+1], *p=buf+sizeof(buf);
for (*--p=0; x; x>>=1) *--p='0'+x%2;
 
    
    
        R.. GitHub STOP HELPING ICE
        
- 208,859
- 35
- 376
- 711
- 
                    You at least should use `+` instead of `|` for pure portability, since `'0'` is not guaranteed to be even. `x&1` and `x%2` are 100% identical for unsigned operands so it's purely a matter of preference. – R.. GitHub STOP HELPING ICE Dec 07 '10 at 18:32
- 
                    1
1
            
            
        I don't know but I'd make something like
int[32] bits = {};
int     value = 255;
int     i = 0;
while (value)   
{
  bits[i++] = value & 1;
  value = value >> 1;
}
 
    
    
        Paulo Santos
        
- 11,285
- 4
- 39
- 65
- 
                    @6502: a small infinite loop followed by a segmentation fault, I suppose. – ruslik Dec 07 '10 at 17:51
- 
                    
- 
                    @ruslik, it won't enter in an infinite loop. Remember how the shift works. – Paulo Santos Dec 07 '10 at 17:54
- 
                    1the standard does not specify what right shift to use for signed values. AFAIK, x86 compilers uses arithmetic shift. Declare `value` as `unsigned` to avoid it. – ruslik Dec 07 '10 at 17:56
1
            
            
        A simple and fast way is to use an unsigned integer as a "cursor" and shift it to advance the cursor:
1000000000000000
0100000000000000
0010000000000000
0001000000000000
0000100000000000
0000010000000000
0000001000000000
0000000100000000
...
At each iteration, use bitwise & to see if the number and the cursor share any bits in common.
A simple implementation:
// number of bits in an unsigned int
#define BIT_COUNT (CHAR_BIT * sizeof(unsigned int))
void toBits(unsigned int n, int bits[BIT_COUNT])
{
    unsigned int cursor = (unsigned int)1 << (BIT_COUNT - 1);
    unsigned int i;
    for (i = 0; i < BIT_COUNT; i++, cursor >>= 1)
        out[i++] = (n & cursor) ? 1 : 0;
}
 
    
    
        Joey Adams
        
- 41,996
- 18
- 86
- 115
 
    