Download presentation
Presentation is loading. Please wait.
Published byHelena Stokes Modified over 8 years ago
1
Bitwise Operators Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu
2
Bitwise Operators “Mathematics may be the language of scientists, but binary is the language of Computer Scientists” Dr. David A. Gaitros
3
Binary Operations Recall the following: – A bit is the smallest digit capable of being stored in a modern day digital computer. – A byte, consisting of 8 bits is the smallest unit of storage that a computer can work with. – A word ( usually somewhere between 32 and 64 bits) is the smallest addressable item in a computer. The word size of a computer is usually dictated by either the bus size of the system or the word size of the CPU.
4
Bitwise Operators OperatorNameDescription & bitwise AND1 only if both operands are 1. |bitwise OR1 if ether or both operands are 1 ^bitwise exclusive or1 if either but not both operands are 1 <<Left ShiftShifts bits of the first operand by the number of bits specified by the second operand. >>Right ShiftShifts bits of the first operand by the number of bits specified by the second operand. ~ComplementFlips the bits in the operand. All 1’s become 0’s and all 0’s become 1’s.
5
Bitwise Operators Bitwise operators are used to directly manipulate the bits of integral operands such as char, short, int, long (both signed and unsined). Normally unsigned integers are used when dealing with bitwise operations.
6
Bitwise Operations Code Example: #include using namespace std; int main(void) { unsigned x=10; unsigned y=0; y = x | y; return 0; } // What does this do? // Nothing really.
7
Bitwise Operators Shortcuts X &= y; same as x = x & y; x |= y; same as x = x | y; x ^= y; same as x = x ^ y; x << y; same as x = x << y; x >> y; same as x = x>> y;
8
Bitwise Operations Why use them? – A single bit shift left is a very fast way of integer multiplication by 2. – A single bit shift right is a very fact way of integer division by 2. – An AND operation with all zeros is a very efficient way to clear out a field. – We can use single bits in a word (32 bits) to represent Boolean values very efficiently. These are sometimes call flags.
9
Bitwise Operators short x = 6891; short y = 11318; Internal Representation x: 00011010 11101011 y: 00101100 00110110
10
Bitwise Operators // Logical AND operator x: 00011010 11101011 y: 00101100 00110110 -------------------- 00001000 00100010
11
Bitwise Operators // Logical OR operator x: 00011010 11101011 y: 00101100 00110110 ----------------- 00111110 11111111
12
Bitwise Operators // Logical XOR // (exclusive OR) x: 00011010 11101011 y: 00101100 00110110 ------------------- 00110110 11011101
13
Bitwise Operators // Shift Left 2 x: 00011010 11101011 ------------------------ 01101011 10101100 // Shift right 4 y: 00101100 00110110 ----------------- 00000010 11000011
14
Bitwise Operators // And here is the // complement of x (~x) x: 00011010 11101011 ------------------ ~x: 11100101 00010100
15
Bitwise Operators // So.. How do I set // a bit? void SetBit(int num) { int mask=1<<num; flags = flags | mask; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.