5.3.5. Bitwise and Shift Operators (optional) PDF

Title 5.3.5. Bitwise and Shift Operators (optional)
Course Introduction To Algorithms And Data Structure
Institution East Carolina University
Pages 2
File Size 89.6 KB
File Type PDF
Total Downloads 11
Total Views 152

Summary

Spring 2016...


Description

5.3.5. Bitwise and Shift Operators (optional) Integers are represented in binary (base 2). For example, an 8 bit integer 3 is represented as 00000011. Some operators work on integers by looking directly at their binary representations. x&y This computes the bitwise and of integers x and y. It works on each bit separately, treating 0 as false and 1 as true. For example, the bitwise and of 8-bit integers 00001111 and 01010101 is 00000101. Notice that first bits have been and-ed together, the second bits have been and-ed together, etc. This is typically used for masking. One of the operands is the mask; a 0 represents a bit that will be 0 in the result and a 1 indicates a bit that will be taken from the other operand in the result. If x is a 32-bit integer then x & 0x000000FF yields the rightmost 8 bits of x.

x|y This computes the bitwise or of x and y. For example, 01000000 | 00000100 yields 01000100. This is typically used for combining bits.

~x This is the bitwise complement of x. It just replaces each 0 by 1 and each 1 by 0.

x^y This is the bitwise exclusive-or of x and y. The result has a 1 in each position where exactly one of x and y has a 1.

x n

This is the result of shifting xto the right n bits. Typically, the leftmost n bits of the result are the same as the leftmost bit of x. For example, 10101010 >> 4 yields 11111010....


Similar Free PDFs