Number Bases and C's Bitwise Operators
Number Bases and C's Bitwise Operators
lec3.tex 1
Exercise: Convert the decimal number 86 to a binary num- Hexadecimal and Octal Constants in C
ber.
We can use hexadecimal notation in C programs by
prefixing the constant using the characters ’0x’. The
Hexadecimal Representation hex digits may be in upper of lower case. For exam-
Binary numbers are too verbose for many purposes ple, the constant 0x21 has the value 33 (decimal).
so we often use use hexadecimal (base 16) numbers. C also has octal (base 8) constants which are de-
Hex numbers are less verbose but also easier to con- noted by prefixing a number with a zero. For exam-
vert to binary. Since the base is 16, we need 16 dif- ple the constant 010 has the value 8 (decimal). Octal
ferent digits. We use the digits 0 to 9 and the letters A notation is no longer widely used.
to F. The following table shows the the 16 hexadec- Unfortunately, C does not have binary constants.
imal digits and the corresponding values in decimal
and binary.
C’s Bitwise Logical Operators
decimal binary hex
0 0000 0 These operators operate bit-by-bit on the binary rep-
1 0001 1 resentation of their operands.
2 0010 2 The bitwise complement operator, ˜, is a unary op-
3 0011 3 erator similar to the logical negation operator and has
4 0100 4
the same precedence. However, it inverts the values
5 0101 5
6 0110 6 of the bits in the binary representation of the operand.
7 0111 7 If a bit is 0, the bitwise negation sets that bit in the
8 1000 8 result to 1 and vice versa. For example, ˜ 2 has the
9 1001 9 value 1 (2 is 10 and 1 is 01 in binary).
10 1010 A The bitwise ‘and’, ‘exclusive-or’1 and ‘or’ oper-
11 1011 B ators are &, ˆ, and |. They result in the operation
12 1100 C being applied to the bits in the binary representa-
13 1101 D
tions of their operands. Both operators have lower
14 1110 E
15 1111 F precedence than the comparison operators but higher
precedence than the logical operators. The bitwise
To convert from binary to hex we group the bi- ‘and’ has a higher precedence than the bitwise ‘or’.
nary digits into groups of 4 starting from the least Exercise: What are the values of the following expressions?
significant (rightmost) digit. The we just look up the
corresponding hexadecimal value in the table. ( 7 ˆ 5 ) | 5
Exercise: Convert the binary number 1101000 to hexadeci- 0xAA & 15
mal.
Hex notation is used because the conversion be- Note that there is an important difference between
tween hex and binary is much simpler than between logical and bitwise logical operators. For example,
decimal and binary. This is becase each hex digit the expression 5 && 2 is 1 while 5 & 2 is 0.
represents exactly 4 bits. Exercise: Why?
To convert a decimal number to hex you can first
convert the number to binary and then group the bits Bit Shift Operators
into groups of 4 bits starting from the right. These
4-bit patterns are then easily converted to hex digits. C also has bit-shift operators. These operators shift
Similarly, to convert a hex number back to deci- the bits in the left operand left (<<) or right (>>)
mal first convert it to a binary number and find the by the number of bit positions given by the second
corresponding decimal value. operand. For example, x>>2 shifts bits in the binary
Exercise: What are the binary and decimal representations 1 The exclusive or operator gives 0 if the two bits are the same
2
representation of x right by 2 bits. If x had the value
0x4 (0100 binary), the value of x>>2 is 0x1 (0001).
Exercise: What is the value of 0x4 >> 2? How about 0x4
<< 1? How does shifting the bits in a number left by 1 position
affect the value of a number? How about shifting them right?
Each size of integer can only hold a limited num-
ber of bits (8, 16 or 32). Thus each shift causes one
bit to be shifted “off the end” and another bit to be
shifted in at the other end. The bit that is shifted
out disappears. For unsigned integers a zero is al-
ways shifted in. For right shifts on signed integers
the value of the most significant bit is duplicated and
shifted in.