Lazyworm said:
given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?
int somevariable = 123;
unsigned char varp = (unsigned char *)&somevariable;
Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
that can be used to access the individual bytes of the value.
Note the use of unsigned char, not of char. unsigned char is
guaranteed not to have any trap values or padding bits: provided
that the underlying data representation really is binary
(not, for example, trinary on some bizarre computer) then
unsigned char is the only type that you can use to be sure that you
can access the individual bits.
But be careful: varp[0] does not necessarily contain the
"most significant byte" of the value you wish to examine, and
it does not necessarily contain the "least significant byte" either:
varp[0] could end being something in the "middle" of how that
particular systems treats "int" values (seriously!). And there
can be padding bits in numeric values, and there can be padding
bits in structures that use bitfields, and there can be padding
bytes in structures -- so although you might be able to examine
the underlying bytes, it might not always be immediately clear what
those bytes *mean*.