Download presentation
Presentation is loading. Please wait.
Published byJoshua Watts Modified over 9 years ago
1
Bitwise Operations CSE 2451 Rong Shi
2
Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label – Valid digits: 0-9 – int x = 22; Hexadecimal (2 4 or 4 bits) – Starts with 0x – Valid digits: 0-9 and A-F – int y = 0xFF12; Octal (2 3 or 3 bits) – Starts with 0 – Valid digits: 0-7 – int w = 067; Binary (2 1 or 1 bit) – Starts with 0b – Valid digits: 0,1 – int z = 0b01001;
3
Bitwise Operations Operate on the bits of a data word Corresponding bits of both operands are combined by the usual logic operations Apply to integer types, not floats
4
Why use bitwise operators? Cryptography Compression algorithms Computer graphics Embedded devices and data storage efficiency Hash functions Network protocols Not necessarily faster than arithmetic operations – Some nifty tricks though Some nifty tricks though
5
Bitwise operators & ANDResult is 1 if both operand bits are 1 | ORResult is 1 if either operand bit is 1 ^ Exclusive ORResult is 1 if operand bits are different ~One’s Complement Each bit is reversed <<Shift leftMove every bit left (multiply by 2) >>Shift right Move every bit right (divide by 2)
6
Examples (assuming an int is only 8-bits) int c, a, b; a = 0b11110000; b = 0b10101010; c = a & b; // 1010 0000 c = a | b; // 1111 1010 c = a ^ b; // 0101 1010 c = ~a; // 0000 1111 c = a << 2; // 1100 0000 c = a >> 5; // 0000 0111 What are a and b’s decimal values? Depends on if the leftmost or rightmost is the most significant bit. On stdlinux a is 240 b is 170
7
AND and OR usage: bit masking
8
XOR usage: swapping temp = x; x = y; y = temp; x = x ^ y; y = x ^ y; x = x ^ y; Additional XOR uses and a proof for the validity of swapping
9
Bit shifts and overflows Shift k bits : multiply or divide by 2 k – New bits “shifted in” are zeroes Some bit shifts are poorly defined, their results are implementation dependent – a << -5 – a << 493
10
Looping using bitwise operations int copy = n; while (copy != 0) { // …do stuff with copy… // how would you “access” the least significant bit? // move every bit right copy = copy >> 1; }
11
Code example #include void main() { unsigned int a,b,c,d,e; a = 0xF0F0; b = 0x5555; c = a >> 4; d = b >> 4; e = 0b01000010; // %x is unsigned int in hex printf("a is %x\n",a); printf("b is %x\n",b); printf("a >> 4 is %x\n",c); printf("b >> 4 is %x\n",d); printf("binary = %x\n",e); } Output is: a is f0f0 b is 5555 a >> 4 is f0f b >> 4 is 555 binary = 42 Note: printf does not directly support printing values in binary or octal form, only decimal and hex
12
Traditional Bit Definition #define EMPTY 01 #define JAM 02 #define LOW_INK 16 #define CLEAN 64 char status; Example statements: if (status == (EMPTY | JAM))...; if (status == EMPTY || status == JAM)...; while (! status & LOW_INK)...; int flags |= CLEAN /* turns on CLEAN bit */ int flags &= ~JAM /* turns off JAM bit */
13
Traditional Bit Definitions Used very widely in C – Including a lot of existing code No checking – You are on your own to be sure the right bits are set Machine dependent – Need to know bit order in bytes (8 bits), byte order in words (2, 4, … bytes) Working with individual bits – Use AND and shift to extract – Use shift and OR to insert
14
Additional (non-bitwise) operators
15
Conditional Operator – If/Then/Else Ternary operator? : Syntaxbool ? exp1 : exp2 Evaluation – If bool is true (non-zero), the result is exp1 – If bool is false (zero), the result is exp2 Example: int a = 10; int b = 20; x = a > b ? a : b;// x = b since a is less than b Style – Rarely more readable than using if/else – Useful in macros – ex: #define MAX(a, b) (((a)>(b)) ? (a) : (b))
16
Comma operator (when used in arithmetic expressions) Syntaxexp1, exp2, exp3, …, expn Evaluate statements from left to right, result of the entire expression is expn (rightmost exp) Example: // x is set to 10, y is set to 5, and value is set to 15 value = (x = 10, y = 5, x + y); Comma operator has lowest precedence – use parenthesis Style – Rarely more readable than using multiple single expressions – Can be used to cause “side effects” Side effect – a statement that modifies a variable or program state in addition to returning a value Ex: while (c=getchar(), c!= ‘9’) Usually result in code that is less readable
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.