# Bit Fields & Bitwise Operations - WPI

Bit Fields & Bitwise Operations CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 1 Bitwise Operations See 2.9 and 6.9 in Kernighan & Ritchie Many situation, need to operate on the bits of a data word Register inputs or outputs Controlling attached devices Obtaining status Especially ECE-2801 and ECE-3803 CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 2 Review Bitwise Operations in Integers

Corresponding bits of both operands are combined by the usual logic operations. & AND ~ Complement Result is 1 if both operand bits are 1 | OR Result is 1 if either operand bit is 1 ^ Exclusive OR Result is 1 if operand bits are different CS-2301, B-Term 2009 Each bit is reversed << Shift left Multiply by 2 >> Shift right Divide by 2 Apply to all kinds of integer types: Signed and unsigned 3

Bitchar, Fields &short, Bitwise Ope int, long, long long rations Examples a 1 1 1 1 0 0 0 0 unsigned int c, a, b; c = a & b; c = a | b; c = a ^ b; c = ~a; b 1 0 1 0 1 0 1 0 c = a << 2; c = a >> 3; CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 4 Right Shift is Tricky unsigned int c, a; c = a >> 3;

signed int c, a, b; c = b >> 3; c = a >> 3; CS-2301, B-Term 2009 a 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 b 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 a 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 Bit Fields & Bitwise Ope rations 5 Two Approaches Traditional C Use #define and a lot of bitwise operations Modern Use bit fields CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 6

Example Printer Status Register ty er p Em p Pa in an w Lo e Cl pe m pa ja

k r Traditional C definition of bit fields #define #define #define #define CS-2301, B-Term 2009 EMPTY JAM LOW_INK CLEAN 01 02 16 64 Bit Fields & Bitwise Ope rations 7 Example Printer Status Register (cont.) ty

er p Em p Pa in an w Lo e Cl pe m pa ja k r

Traditional bit fields (continued) char status; 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 */ CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 8 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, byte order in words Integer fields within a register Need to AND and shift to extract Need to shift and OR to insert CS-2301, B-Term 2009

Bit Fields & Bitwise Ope rations 9 Example Printer Status Register (cont.) ty er p Em p Pa in an w Lo e Cl coun t

pe m pa ja k r An integer field (traditional style) #define COUNT (8|16|32|64|128) int c = (status & COUNT) >> 3; status |= (c << 3) & COUNT; CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 10 Modern Bit-Field Definitions See Kernighan & Ritchie, 6.9 Like a struct, except Each member is a bit-field within a word Accessed like members of a struct Fields may be named or unnamed

Machine-dependent Order of bits in word Size of word CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 11 Modern Bit-field Definitions ty er p Em p Pa in an w Lo

e Cl pe m pa ja k CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 12 r struct statusReg { unsigned int emptyPaperTray :1; unsigned int paperJam :1; :2; unsigned int lowInk :1; :1;

unsigned int needsCleaning :1; :1; }; Example Printer Status Register (cont.) pe m pa ja r Bit Fields & Bitwise Ope rations ty er k struct statusReg { unsigned int emptyPaperTray :1; unsigned int paperJam :1; :1; unsigned int count

:5; :1; unsigned int lowInk :1; :1; unsigned int needsCleaning :1; :1; }; CS-2301, B-Term 2009 p Em p Pa in an w Lo e Cl coun t 13

Modern Bit-fields (continued) struct statusReg s; if (s.empty && s.jam) ...; while(! s.lowInk) ...; s.needsCleaning = true; s.Jam = false; int c = s.count; s.count -= 1; CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 14 Questions about Bit Fields? CS-2301, B-Term 2009 Bit Fields & Bitwise Ope rations 15

## Recently Viewed Presentations

• Dichotomous Keys. Organizing Life. TEKS/ TAKS. 8A. collect and classify organisms at several taxonomic levesl such as speies, phylum, and kingdom . using dichotomous keys. 8B . analyze relationships. among organisms and develop a model of a heirarchical classification system...
• (Note, use the alignment sighting line to aim the receiver's sensor in the direction of the opposite tree where the Transmitter unit will be mounted.) Recommended Tools: Screwdriver, (Philips head) Step 3. Turn "on" the Receiver unit's power switch and...
• Sidonio Apollinare, Epistula I, 10- 14 (Arles, a. 461): Postridie jussit Augustus (Maioranus) ut epulo suo circensibus ludis interessemus. Primus jacebat cornu sinistro consul ordinarius Severinus, vir inter ingentes principum motus atque inaequalem reipublicae statum gratiae semper aequalis.
• Big Question: How does technology help adventurers reach new places? Monday. Tuesday. Wednesday. ... ooze - soft mud or slime, ... parts and sensing devices controlled by a computer. Vocabulary Words. sediment - material that settles to the bottom of...
• Bantu Migration. Bantu Migration Map. Slash and burn method of farming forced them to move every few years. When they moved, they shared agricultural & ironworking skills with the new people they encountered. Migrate across central Africa & eventually make...
• When you are in the narrators head. You see the story through their eyes. We know the narrators thoughts. "I" and "We" are key words to look for. These narrators can be reliable or unreliable. A very personal form of...
• The palaeoclimatic record from eastern Bolivia Late Holocene expansion of Amazonian rainforests The palaeo-climatic record from east-central Brazil The palaeo-climatic record from north-eastern Colombia Long core sites (Australia-Indonesian region) Long cores: Australia-Indonesia PowerPoint Presentation Holocene climate change in East Africa...
• Name a topographic structure that is represented by a contour line that closes within the limits of its map. Tell me the altitude at sea level. Index Contour Steepness Practice Map Review Can intervals between contour lines on the same...