## Saturday, 3 March 2012

### Numbering Notations in C & C++

C has a method for telling the compiler whether a number is in decimal, hexadecimal or octal. That’s base 10, base 16 and base 8. If I want to assign a number to a variable I have my choice of those three number systems. If I’m interested in binary numbers, I’d have to make up some code to deal with it. You can’t assign a binary number to a variable directly.

Since C++ builds on C, the same notation applies.

I wrote some test code to confirm how it works. What’s interesting is the output for negative numbers in hex and octal. Have a look.

Test Code.

I created a console application in Visual C++ 2010 to test the code.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library
#include <cstdio>

//
int main()
{
int x;

printf("printf Specifiers\n\n", x);
printf("Decimal numbers: %%d (integers)\n");
printf("Hex numbers: %%x or %%X (lowercase, uppercase)\n");
printf("Octals: %%o \n\n");

printf("Positive numbers \n\n", x);

// Decimal 10.
x = 10;
printf("Assign x = 10; \n");
printf("Here is a decimal number: %d \n\n", x);

// Hex equiv.
x = 0x0A;
printf("Assign x = 0x0A; \n");
printf("Hex numbers begin with 0x (zero X). \n");
printf("Here is the hex equivalent: %X \n", x);
printf("Not sure why it's not diplaying as 0x0A. \n\n", x);

// Octal. Starts with zero.
x = 012;
printf("Assign x = 012; \n");
printf("Here is the octal equivalent: %o \n", x);
printf("Octals beging with '0' (zero). \n\n\n", x);

printf("Negative numbers \n\n", x);

x = -10;
printf("Assign x = -10; \n");
printf("Here is a decimal number: %d \n\n", x);

x = -0x0A;
printf("Assign x = -0x0A; \n");
printf("Here is the hex equivalent: %X \n", x);
printf("The minus sign goes before everything as in -0x0A.\n\n", x);

printf("Assign x = -012; \n");
printf("Here is the octal equivalent: %o \n\n", x);

x = 0xfffffff6;
printf("Assign x = 0xfffffff6; \n");
printf("Here is a decimal number: %d \n", x);
printf("Here is the hex equivalent: %X \n", x);
printf("Here is the octal equivalent: %o \n\n", x);

x = 10;
printf("Assign x = 10; \n");
printf("Here is a decimal number: %d \n", x);
printf("Here is the hex equivalent: %X \n", x);
printf("Here is the octal equivalent: %o \n", x);

// keep console window open
system("pause");

// return some value
return 0;
} // end main

Minus ten in decimal is -10, but in hex it’s FFFF FFF6 and in octal it’s 37 777 777 766. Those look like junk numbers, but they are all -10. Here’s why.

A signed integer uses the two’s complement binary number system. That means the left most bit is used as a sign bit (0 for positive and 1 for negative). The remaining bits are used to represent the number.

To simplify things. I’ll use an 8-bit integer. Here’s the layout.

Each bit represents a power of 2. From right to left it's:
7          6          5          4          3          2          1          0
128      64        32        16        8          4          2          1

If I add up all those numbers (128 to 1) I get 255. So, 0 to 255 is 256 possible signed numbers. The maximum number of combinations is 2^8 or 256.

Plus seven in binary is: 0 0 0 0  0 1 1 1

7          6          5          4          3          2          1          0
0          0          0          0          0          4          2          1

The sum of 4, 2, 1 is 7 or 0x07 or 07

The largest negative number is 2^(8-1) or 128. The largest positive number is 2^(8-1)-1 or 127. The range is therefore -128 to 127.

Minus seven in binary is: 1 1 1 1  1 0 0 1

7          6          5          4          3          2          1          0
128      64        32        16        8          0          0          1

The sum of those numbers is: 249. When you subtract 256 (2^8) you end up with -7 or 0xF9 or 0371.

You can alternatively add the numbers this way.

7          6          5          4          3          2          1          0
-128     64        32        16        8          0          0          1