{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week11-Bits

# Week11-Bits - Bit arrays King 20.1 20.2 Bit strings Signal...

This preview shows pages 1–11. Sign up to view the full content.

Bit arrays King: 20.1, 20.2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Bit strings Signal mask and file descriptor sets are implemented using bit array or bit strings. You should always use the supplied functions macros to manipulate these structures. It is useful to know how they are implemented. Each bit represents an element of the set – 1 == in the set – 0 == not in the set
Bitwise operators • shift (note that bits fall off the ends) << left shift >> right shift i = 6; /* 0000 0000 0000 0110 */ j = i << 2 /* 0000 0000 0001 1000 */ k = i >> 2 /* 0000 0000 0000 0001 */ – set bit at index 10 (start indexing at 0) j = 10; i = 1 << j /* 0000 0100 0000 0000 */

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Bitwise Complement, And, Or Xor ~ complement • & and ^ xor • | or i = 17; /* 0001 0001 */ j = 3; /* 0000 0011 */ k = ~j; /* 1111 1100 */ m = i & j /* 0000 0001 */ n = i | j /* 0001 0011 */ o = i ^ j /* 0001 0010 */
Idioms Setting a bit string to all 1s: –i = ~0; or i = -1; Set all but the last 2 bits to 1: –i = ~0x3; Setting bit j –x = 1 << j; –or –x = 0; –x |= 1 << j;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Options, Masks, or Flags Flags are often implemented as a bit mask • Example: open(“temp”, O_WRONLY | O_CREAT); #define O_RDONLY 00

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}