Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,

Similar presentations


Presentation on theme: "Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,"— Presentation transcript:

1 Bit Manipulation when every bit counts

2 Questions on Bit Manipulation l what is the motivation for bit manipulation l 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. l 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 ? l What is called a mask and how is it used? l what are bit fields? 2

3 Why Bit Manipulation l extremely efficient and compact way of data storage and processing l a way of low-level interaction with peripheral devices: mice, disks, scanners, etc. l memory and compute cycles are cheap, bit manipulating programs are cryptic. Do not use unless you have to 3

4 Bits l Bit - elementary unit of information storage n can store either 1 or 0 l Electronic circuits are most robust and dependable if they have at most two stable states n computer data is stored and processed as sequences of bits l more complex data types are encoded in bit sequences n How are signed/unsigned integers are represented? Floating point data? Characters? l 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 l octal and hexadecimal simplify conversion to binary base n octal digit represents three binary digits 012 means 001 010 (binary) n hexadecimal – four binary digits 0x12 means 0001 0010 (binary) 4

5 Conversion with Decimal Base l 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 l 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

6 Input/Output in Bases l a number may be input or printed in decimal (default) octal or hexadecimal using stream manipulators dec - decimal oct - octal hex – hexadecimal l to use – input/output manipulator to stream cin >> oct >> myNumber; // inputs in octal cout << hex << myNumber; // outputs in hexadecimal l remember the same number can be input/output using any of the bases 6

7 Bitwise Operations l treat data as a sequence of bits rather than complex data type unsigned (int) – good data type to use l 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, 1 s 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

8 Bit Fields l can specify fewer bits in integer member variables of structures and classes l 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


Download ppt "Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,"

Similar presentations


Ads by Google