By Dan Gookin

Happily, you don’t have to program any digital device by writing low-level code, flipping switches, or soldering wires. That’s because today’s C programming happens at a higher level. But still, deep within the machine, that type of low-level coding continues. You’re just insulated from the primordial soup of ones and zeros from which all software rises.

Introduction to binary

The binary digits, or bits, are 1 and 0. Alone, they’re feeble; but in groups, they muster great power. Digital storage uses these bits in groups.

Binary Groupings
Term C Variable Bits Value Range Unsigned Value Range Signed
Bit _Bool 1 0 to 1 0 to 1
Byte char 8 0 to 255 –128 to 127
Word short int 16 0 to 65,535 –32,768 to 32,767
Long long int 32 0 to 4,294,967,295 –2,147,483,648 to 2,147,483,647

The advantage of grouping bits into bytes, words, and so on is that it makes them easier to handle. The processor can better deal with information in chunks. How chunks get their values is based upon powers of 2.

Powers of 2
Expression Decimal Value Binary Value
20 1 1
21 2 10
22 4 100
23 8 1000
24 16 10000
25 32 100000
26 64 1000000
27 128 10000000

You see the range of values that can be stored in 8 bits, or 1 byte. It’s the same range you’d find in a C language char variable. Indeed, if you total Column 2, you get 255, which is the number of bits in a byte.

Actually, you’ll find 256 possible values for a byte, which includes the all-zero permutation. That’s a value as well.

Below shows how the powers of 2 map into binary storage. Just as decimal places in a base 10 number increase by powers of 10, bits in a binary number increase by powers of 2, reading from right to left.

image0.jpg

Each bit that’s set, or has the value 1, represents a power of two: 25, 23, 21, and 20. When you multiply these values by their decimal counterparts and then total them up, you get the decimal representation of binary 00101011, which is 43.

That’s all well and good, but please don’t memorize it!

  • Don’t concern yourself with translating binary into decimal values; the computer does that job for you all the time. Indeed, the computer sees only binary and then displays decimal numbers as a courtesy for your human eyeballs. But when you manipulate binary values, it helps to know what’s going on.

  • Changing a bit’s value to 1 is referred to as setting the bit.

  • Changing a bit’s value to 0 is referred to as resetting a bit.

How to display binary values

To best make sense of the C language’s binary manipulation operators, it helps to see a binary number in action. The printf() function lacks a binary conversion character, and the C library doesn’t host a binary output function. Nope, to view a binary number, you have to craft your own function.

The binbin() Function presents a binary output function called binbin(). The binbin() function, at Line 15 in The binbin() Function, swallows an int value. Its output is a string representing that int value in binary digits.

THE BINBIN() FUNCTION

#include <stdio.h>
char *binbin(int n);
int main()
{
 int input;
 printf("Type a value 0 to 255: ");
 scanf("%d",&input);
 printf("%d is binary %sn",input,binbin(input));
 return(0);
}
char *binbin(int n)
{
 static char bin[9];
 int x;
 for(x=0;x<8;x++)
 {
 bin[x] = n & 0x80 ? '1' : '0';
 n <<= 1;
 }
 bin[x] = '';
 return(bin);
}

Generally speaking, the contents of the binbin() function appear rather mysterious. That’s okay.

Exercise 1: Type the source code from The binbin() Function into a new project. Build and run it a few times to see how integers appear as binary numbers.

As written in The binbin() Function, binbin() displays only 8 bits of data, though the int variable type typically stores many more bits.

Exercise 2: Modify the binbin() function from The binbin() Function so that it displays 16 bits of the int value. (Well, technically, 16 bits is a short int.) To do so, you need to change these items:

Line 9: Alter the text so that 65535 is specified instead of 255.

Line 17: Modify the size of the array to 17 to account for 16 characters in the output plus the (null character) at the end of the string.

Line 20: Adjust the immediate value 8 in the code to 16 to account for all 16 characters in the output.

Line 22: Replace the value 0x80 with 0x8000. This change makes the bit field larger.

Build Exercise 2. Run it a few times to see what the bit field looks like for larger values.