Bit Manipulation
description
Transcript of Bit Manipulation
Bit Manipulation
when every bit counts
Questions on Bit Manipulation
what is the motivation for bit manipulation what is the binary, hexadecimal, octal numbers? why are they used? what are the following numbers in decimal 012, 0b111, 0xA1, 23?
Convert 11 decimal into binary, octal, hexadecimal. What is bitwise OR, bitwise AND, bitwise exclusive OR, bitwise
complement, left and right shift? how are they denoted and how are they computed?
what is sizeof? What is called a mask and how is it used? what are bit fields?
2
Why Bit Manipulation
extremely efficient and compact way of data storage and processing
a way of low-level interaction with peripheral devices: mice, disks, scanners, etc.
memory and compute cycles are cheap, bit manipulating programs are cryptic. Do not use unless you have to
3
Bits Bit - elementary unit of information storage
can store either 1 or 0 Electronic circuits are most robust and dependable if they have at most two
stable states computer data is stored and processed as sequences of bits
more complex data types are encoded in bit sequences How are signed/unsigned integers are represented? Floating point
data? Characters? base for numeric representation can be binary, decimal, octal or
hexadecimal octal constant has leading 0: 012 == 10 hexadecimal has leading 0x: 0x12 == 18 binary has leading 0b: 0b11 == 3
octal and hexadecimal simplify conversion to binary base octal digit represents three binary digits
012 means 001 010 (binary) hexadecimal – four binary digits
0x12 means 0001 0010 (binary)4
Conversion with Decimal Base to decimal
0651 = 6*8^2 + 5*8^1 + 1*8^0 = 425
0x1B6 = 1*16^2 + 11*16^1 + 6*16^0 = 438
0b10101 = 1*2^4 + 1*2^2 + 1*2^0 = 21
from decimal
425 %8 438 %16 21 %2
53 1 27 6 10 1
6 5 1 11 5 0
------ -------- 2 1
0651 0x1B6 1 0
------- 0b10101
5
Input/Output in Bases
a number may be input or printed in decimal (default) octal or hexadecimal using stream manipulators
dec - decimal
oct - octal
hex – hexadecimal to use – input/output manipulator to stream
cin >> oct >> myNumber; // inputs in octal
cout << hex << myNumber; // outputs in hexadecimal remember the same number can be input/output using any of the
bases
6
Bitwise Operations treat data as a sequence of bits rather than complex data type
unsigned (int) – good data type to use operations
& - bitwise and: 1 only if both operands are 1
101111
&011010
001010 | - bitwise or: 1 if at least one operand is 1 ^ - bitwise exclusive or: 1 only if exactly one operand is 1 ~ - bitwise complement: single operand, 1s to 0, 0 to 1 << - left shift: shifts bits to left (by specified number of positions), right bits are filled
with 0-oes: if variable a is 0010 then (a << 2) is 1000– this operation looks familiar, what is it?
>> - right shift: shifts bits to right, left bits are filled with 0-oes compound assignment is allowed for all operations: a &=0x001; useful function: sizeof(type or class)
7
Bit Fields can specify fewer bits in integer member variables of structures and
classes example
class card {
private:
unsigned face : 4; // 4 bits; 0-15
unsigned suit : 2; // 2 bits; 0-3
unsigned color : 1; // 1 bit; 0-1
};
8