Game Development Reference
In-Depth Information
Table 2-3 shows the bit values for an unsigned char . When we have an unsigned char variable that
stores the value 255, all of the bits are turned on . This would be represented by 11111111 in binary.
If we add a 1 to this number the values will wrap and we will end up back at 00000000 as the 8 bits
cannot store a higher value. This is something that you will need to be aware of when using unsigned
integer variables.
When you want to represent negative numbers using signed variables, the processor will use Two's
Complement . Two's Complement works by using the highest bit to represent whether the number is
positive or negative. The highest positive number that can be stored in a char variable is 127 and is
represented in binary by 01111111. Adding 1 to this number gives us 10000000, which is -128 and
once again shows you how wrapping occurs in practice.
Switching between a positive and negative number is a simple process. You have to switch all of the
bits and add one. The number 1 is represented in binary by 00000001. To flip 1 to -1 we first flip all
of the bits to 11111110 (which is -2) , then add 1 to give us 11111111.
This is a brief introduction to how a processor represents the integer values your programs will store
in memory. We will cover some more binary representations of numbers in Chapter 3 when we look
at binary operators.
Floating Point Numbers
Floating point numbers in C++ are used to represent numbers with a decimal point. Examples of
these are -10.5, 0.3337, and 89.8376.
Like the signed integer types, floats can be used to store positive and negative numbers. Unlike
integers, though, there are no unsigned versions of floating point numbers. Defining a floating point
is done in the following way:
float decimalNumber = 1.0f;
When defining floating point variables in code, the value must be followed by an f. The reason for
this is that a number with a decimal point but no f is a double.
The current floating point standards require that floats be represented with 32 bits (or 4 bytes).
Thirty-two-bit floating point numbers might not have a large enough range for what we would like
to achieve under given circumstances, so C++ also supplies the double type. The float type can be
thought of as a single precision floating point number and the double type is then a double precision
floating point number. A double can be defined as follows:
double doubleNumber = 1.0;
Single precision floats are usually sufficient for all but a few scenarios in game development and are
generally faster and more efficient for the types of tasks we carry out.
 
Search WWH ::




Custom Search