Wednesday, October 14, 2009

Printing char value in binary mode at C programing language

Print a char value(ch variable here) in binary:
printf("%c%c%c%c%c%c%c%c",
(bitMap[i]&0x80)?'1':'0',
(bitMap[i]&0x40)?'1':'0',
(bitMap[i]&0x20)?'1':'0',
(bitMap[i]&0x10)?'1':'0',
(bitMap[i]&0x08)?'1':'0',
(bitMap[i]&0x04)?'1':'0',
(bitMap[i]&0x02)?'1':'0',
(bitMap[i]&0x01)?'1':'0');
__________________

2 comments:

autism said...

I think, the code given above is the processor coed for the translation of the numeric value into the binary one. There is the direct allocation to the register of the variable. According to me, there should be some other way to redirect that.

Shahab said...

there are lots of way to get to this, this is just an AND operator with constant numbers which gives you the value of one bit in the byte of char.