csbu-binary-2
Types Representation
Standard Types
C99 realizes that all these rules, sizes and portability concerns can be come very confusing very quickly, as the type names come much more and more.
To help, it provides a series of special types which can specify the exact properties of a variable. These are defined in <stdint.h>
and have the form qtypes_t
where q
is a qualifier, type
is the base type, s
is the width in bits and _t
is an extension so you know you are using the C99 defined types.
For example uint8_t
is an unsigned integer exactly 8 bits wide. Many other types are defined; the complete list is detailed in C99 17.8 or in the header file.
Number Representation
Negative values
Sign Bit
Just use the first bit of a number to sign it’s positive or negative.
One’s Complement
One’s complement simply applies the not operation to the positive number to present the negative number. So, for example the value -90 (-0x5A) is represented by ~01011010 = 10100101
.
Two’s Complement
Two’s complement is just like one’s complement, except the negative representation has one added to it and we discard any left over carry bit. Here -90
would be ~01011010+1=10100101+1 = 10100110
.
Floating Point
A decimal number is represented by sign x significand x 2^exponent
the Bias of Exponent
The exponent needs to be able to represent both positive and negative values, thus an implied value of 127
is subtracted from the exponent. For example, an exponent of 0
has an exponent field of 127
, 128
would represent 1
and 126
would represent -1
.