Title: Bitwise Operators
1Bitwise Operators
2Bitwise Operators (integers)
- Bitwise "and" operator
- Bitwise "or" operator
- Bitwise "exclusive or" operator
- Bitwise "ones complement" operator
- Shift left ltlt
- Shift right gtgt
3Base 2 and 2's complement
2's complement x(-x) 2nwhere n bits
4operators , , ,
The value of each bit is determined only by the
bit(s) in its position
- Examples int a 33333, b -77777
Expression Representation Value
a 00000000 00000000 10000010 00110101 33333
b 11111111 11111110 11010000 00101111 -77777
a b 00000000 00000000 10000000 00100101 32805
a b 11111111 11111110 01010010 00011010 -110054
a b 11111111 11111110 11010010 00111111 -77249
( a b ) 00000000 00000001 00101101 11000000 77248
a b 00000000 00000001 00101101 11000000 77248
5Operators Associativity
() . -gt (postfix) --(postfix) left to right
(unary) -(unary) (prefix) --(prefix) ! sizeof(type) (address) (dereference) right to left
/ left to right
- left to right
ltlt gtgt left to right
lt lt gt gt left to right
! left to right
left to right
left to right
left to right
left to right
left to right
? right to left
- / gtgt etc right to left
,(comma operator) left to right
Operator precedence and associativity - final
look
(ab ltlt 12a gtgt b) is equivalent to
(((ab)ltlt(12a))gtgtb)
6Left Shifts
- short a 0x68ab
- ...
- a ltlt 3 / shift left 3 bits /
Same as a a ltlt 3
Bits positions vacated by shift are filled with
zeros
http//www.harpercollege.edu/bus-ss/cis/166/mmcken
zi/lect19/l19e.htm
7Right Shifts - Unsigned
- unsigned short a 0x98ab
- . . .
- a gtgt 5 / shift right 5 bits /
For unsigned data type, bits positions vacated by
shift are filled with zeros.
http//www.harpercollege.edu/bus-ss/cis/166/mmcken
zi/lect19/l19e.htm
8Right Shifts - Signed (machine dependent)
- short a 0x98ab
- . . .
- a gtgt 5 / shift right 5 bits /
Bit positions vacated by shifting is filled with
a copy of the highest (sign) bit for signed data
type
9Right Shifts (Signed)
- short a 0x78ab
- . . .
- a gtgt 5 / shift right 5 bits /
Bit positions vacated by shifting is filled with
a copy of the highest (sign) bit for signed data
type
10Representations
- char c 'Z'
- int a 1 ltlt 31 / shift 1 to the high
bit / - unsigned b 1 ltlt 31
Expression Representation Action
c 00000000 00000000 00000000 01011010 unshifted
c ltlt 4 00000000 00000000 00000101 10100000 left shifted 4
a 10000000 00000000 00000000 00000000 unshifted
a gtgt 3 11110000 00000000 00000000 00000000 right shifted 3
b 10000000 00000000 00000000 00000000 unshifted
b gtgt 3 00010000 00000000 00000000 00000000 right shifted 3
For signed data types bit positions vacated by
shifting is filled with a copy of the highest
(sign) bit for signed data type
11Implementation Note
- xltltn is equivalent
- to multiplication by 2n.
- xgtgtn is equal to x/2n
- Shifting is much faster than actual
multiplication () or division (/)gt
Multiplications / divisions by powers of 2 should
be implemented using shifts.
0x0001 0x0002 0x0004 0x0008 0x0010 0x0020 0x0040
12Printing the bits of an integer
- int main(void)
- int num0
- do
- printf("Enter an integer\n")
- scanf("d", num)
- bit_print(num)
- putchar('\n')
- while (num!0)
- return 0
Prints the binary representation of an integer.
E.g 00000000 00000000 00000000 00000111(MSB)
(LSB)
13- include ltlimits.hgt
- void bit_print(int a)
- int i
- int n sizeof(int) CHAR_BIT / define
CHAR_BIT 8 (in ltlimits.hgt)/ - int mask 1 ltlt (n - 1)
/ mask 100...0 / - for (i 1 i lt n i)
- putchar((a mask)? '1' '0')
- a ltlt 1
-
- if (i CHAR_BIT 0 i lt n)
- putchar(' ')
-
n is the number of bits in an integer
Prints the most significant bit of a
(condition) ? (if true) (if false) if (amask
0) putchar(0) else
putchar(1)
i 8 i 7
Prints a space between the bytes
14Pack 4 chars into an int
- include ltlimits.hgt
- int pack( char a, char b, char c, char d )
-
- int p a
- p (p ltlt CHAR_BIT) b
- p (p ltlt CHAR_BIT) c
- p (p ltlt CHAR_BIT) d
- return p
Least significant
Most significant
p 0 0 a 0 0 0 0 b 0 0 a b
p 0 0 0 a
p 0 a b 0 0 0 0 c 0 a b c
p a b c 0 0 0 0 d a b c d
15Unpack a byte from an int
- include ltlimits.hgt
- char unpack( int p, int k )
-
- unsigned mask 0xFF
- int n k CHAR_BIT
-
- mask ltlt n
- return ( ( p mask ) gtgt n )
k 0, 1, 2 or 3
n 0, 8, 16 or 24
kth byte is on