In my books, I use the binbin() function to display binary values. It’s a good way to demonstrate some of the C language’s low-level bitwise functions: The binbin() function lets you see the effect of bitwise operations at a binary level, all those ones and zeros.
Over the years, I’ve messed with the binbin() function. Here’s the current version, which I now call binString():
char *binString(unsigned short n) { static char bin[17]; int x; for(x=0;x<16;x++) { bin[x] = n & 0x8000 ? '1' : '0'; n <<= 1; } bin[16] = '\0'; return(bin); }
The binString() function accepts an unsigned short integer as input, variable n
inside the function. The short variable has a bit width of 16, so the string created is 16-characters long. The bin[]
char array holds output string, plus one extra position for the null character, \0
, at the end of the string. It’s a static char array, so its value is retained after the function call.
Within the function, a for loop processes each bit in value n
. The binary &
(AND) operator compares the far left bit with 1 and sets the character ‘1’ or ‘0’ depending on the bit’s value. Value n
is then shifted left one bit position and the loop continues. The result is a string representing the bit values stored in n
, which is returned from the function.
Here is the full code I use to test the binString() function:
#include <stdio.h> char *binString(unsigned short n) { static char bin[17]; int x; for(x=0;x<16;x++) { bin[x] = n & 0x8000 ? '1' : '0'; n <<= 1; } bin[16] = '\0'; return(bin); } int main() { unsigned short value; printf("Enter an integer value, 0 to 65535: "); scanf("%hd",&value); printf("Decimal value is %d\n",value); printf("Octal value is %o\n",value); printf("Hexadecimal value is 0x%X\n",value); printf("Binary value is %s\n",binString(value)); return(0); }
Sample run:
Enter an integer value, 0 to 65535: 43690
Decimal value is 43690
Octal value is 125252
Hexadecimal value is 0xAAAA
Binary value is 1010101010101010
I use the binbin() version of the binString() function in my book, Beginning Programming with C For Dummies, to demonstrate the various bitwise functions in C. The binString() function improves upon it by returning a full string, but the result is the same: a string representation of bits. This complements the natural capability of the C language to output decimal, octal, and hexadecimal.
But what about the other way?
For example, you can input decimal, octal, and hexadecimal values into a C program, but you can’t input binary.
The following code accepts hexadecimal input:
#include <stdio.h> int main() { unsigned n; printf("Type a hex value: "); scanf("%x",&n); printf("You typed %X, which is %d\n",n,n); return(0); }
The %x
directs the scanf() function to read hexadecimal values. Here’s the sample run:
Type a hex value: AA
You typed AA, which is 170
You could change the %x
in the scanf() function to %o
to input octal values or %d
to input decimal values. What you cannot do, however, is use a %
placeholder to read in a binary value. That’s a topic I tackle in next week’s Lesson.