Reputation: 8730
Can someone explain in a bit of detail how the following Char to Binary functions work in C?
Specifically how the bit shifting and logical and work on an iteration
#include <stdio.h>
#include <limits.h>
void printCharAsBinary(char c) {
int i;
for(i = CHAR_BIT; i >= 0; i--){
printf("%d", ( unsigned int )( ( c & (1 << i) ) ? 1 : 0) );
}
}
void printCharAsBinaryVer2(char c){
int bit_index;
for (bit_index = CHAR_BIT; bit_index >= 0; --bit_index)
{
int bit = ( unsigned int ) (c >> bit_index) & 1;
printf("%d", bit);
}
}
You can see them in action here if that helps: http://code.geeksforgeeks.org/zC22WO
Update based on answers:
F
is 70
in decimal
70
is 01000110
in binary
With the following code:
#include <stdio.h>
#include <limits.h>
void printCharAsBinary(char c) {
int i;
for(i = CHAR_BIT; i >= 0; i--){
printf("%d", ( unsigned int )( ( c & (1 << i) ) ? 1 : 0) );
}
}
The iterations will be:
01000110 & 10000000 = 00000000 RESULT is 0 so PRINT 0
01000110 & 01000000 = 01000000 RESULT is > 0 so PRINT 1
01000110 & 00100000 = 00000000 RESULT is 0 so PRINT 0
01000110 & 00010000 = 00000000 RESULT is 0 so PRINT 0
01000110 & 00001000 = 00000000 RESULT is 0 so PRINT 0
01000110 & 00000100 = 00000100 RESULT is > 0 so PRINT 1
01000110 & 00000010 = 00000000 RESULT is > 0 so PRINT 1
01000110 & 00000001 = 00000000 RESULT is 0 so PRINT 0
With the following code:
void printCharAsBinaryVer2(char c){
int bit_index;
for (bit_index = CHAR_BIT; bit_index >= 0; --bit_index)
{
int bit = ( unsigned int ) (c >> bit_index) & 1;
printf("%d", bit);
}
}
00000000 & 00000001 = 00000000 RESULT is 0 so PRINT 0
00000001 & 00000001 = 00000001 RESULT is > 0 so PRINT 1
00000010 & 00000001 = 00000000 RESULT is 0 so PRINT 0
00000100 & 00000001 = 00000000 RESULT is 0 so PRINT 0
00001000 & 00000001 = 00000000 RESULT is 0 so PRINT 0
00010001 & 00000001 = 00000001 RESULT is > 0 so PRINT 1
00100011 & 00000001 = 00000001 RESULT is > 0 so PRINT 1
01000110 & 00000001 = 00000000 RESULT is 0 so PRINT 0
Upvotes: 0
Views: 3613
Reputation: 311048
The type char can behave either as the type signed char
or as the type unsigned char
.
According to the C Standard (6.5.7 Bitwise shift operators)
4 ...If E1 has a signed type and nonnegative value, and E1×2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
5 ... If E1 has a signed type and a negative value, the resulting value is implementation-defined.
Thus in the both functions it would be better to cast the character to the type unsigned char
.
The function can look like it is shown in this demonstrative program
#include <stdio.h>
#include <limits.h>
void printCharAsBinary( char c )
{
for ( int i = CHAR_BIT; i-- != 0; )
{
printf( "%u", ( unsigned char )c >> i & 1 );
}
}
int main(void)
{
for ( char c = 'A'; c <= 'Z'; ++c )
{
printf( "%c: ", c );
printCharAsBinary( c );
putchar( '\n' );
}
return 0;
}
Its output is
A: 01000001
B: 01000010
C: 01000011
D: 01000100
E: 01000101
F: 01000110
G: 01000111
H: 01001000
I: 01001001
J: 01001010
K: 01001011
L: 01001100
M: 01001101
N: 01001110
O: 01001111
P: 01010000
Q: 01010001
R: 01010010
S: 01010011
T: 01010100
U: 01010101
V: 01010110
W: 01010111
X: 01011000
Y: 01011001
Z: 01011010
That is the function sequentially shifts bits of the character starting from the most significant bit to the first position. The binary operator &
sets all other bits except the first one of the resulting value to zero. Thus this expression ( unsigned char )c >> i & 1
extracts the target bit in the given character.
Upvotes: 1
Reputation: 7441
Char has 8 bits in binary representation. You are printing each bit as 1 or 0 in character representation like this:
You go through for loop and with counter you make logical shift to the left and then with bitwise AND you check if bit is set or not. If it is, you print 1, otherwise you print 0.
Lets say your number in binary is 01010101
For loop iterations are:
01010101 & 10000000 = 00000000
result is 0
, you print 0
01010101 & 01000000 = 01000000
result is > 0
, you print 1
01010101 & 00100000 = 00000000
result is 0
, you print 0
After you print everything you get 01010101
.
Upvotes: 2