1 / 15

Bit Fields & Bitwise Operations

This presentation discusses the concepts of bit fields and bitwise operations. It covers topics such as bitwise operations in integers, shift operations, traditional C bit fields, and modern bit-field definitions. Examples and practical applications are also provided.

Download Presentation

Bit Fields & Bitwise Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bit Fields & Bitwise Operations Professor Hugh C. LauerCS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie,Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by BjarneStroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) Bit Fields & Bitwise Operations

  2. Bitwise Operations Especially ECE-2801 and ECE-3803 • See §2.9 and §6.9 in K&R • Many situations, need to operate on the bits of a data word – • Register inputs or outputs • Controlling attached devices • Obtaining status Bit Fields & Bitwise Operations

  3. Bitwise Operations in Integers Corresponding bits of both operands are combined by the usual logic operations. • & – AND • 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 • ~ – Complement • Each bit is reversed • << – Shift left • Multiply by 2 • >> – Shift right • Divide by 2 Apply to all kinds of integer types:–Signed and unsignedchar, short, int, long, longlong Bit Fields & Bitwise Operations

  4. b a 1 1 1 0 1 1 1 0 1 0 0 0 1 0 0 0 Examples unsigned int c, a, b; c = a & b; c = a | b; c = a ^ b; c = ~a; c = a << 2; c = a >> 3; Bit Fields & Bitwise Operations

  5. b a a 1 0 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 Right Shift is Tricky unsigned int c, a; c = a >> 3; signed int c, a, b; c = b >> 3; c = a >> 3; Bit Fields & Bitwise Operations

  6. Two Approaches Much more frequent in real world! • Traditional C • Use #define and a lot of bitwise operations • Modern • Use bit fields Bit Fields & Bitwise Operations

  7. Traditional C definition of bit fields #define EMPTY 01 #define JAM 02 #define LOW_INK 16 #define CLEAN 64 Clean Low ink Paper jam Empty paper Example – Printer Status Register Bit Fields & Bitwise Operations

  8. 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 */ Clean Low ink Paper jam Empty paper Printer Status Register (continued) Bit Fields & Bitwise Operations

  9. 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 ANDand shift to extract • Need to shift and OR to insert Bit Fields & Bitwise Operations

  10. An integer field (traditional style) #define COUNT (8|16|32|64|128) int c = (status & COUNT) >> 3; status |= (c << 3) & COUNT; count Clean Low ink Paper jam Empty paper Printer Status Register (cont.) Bit Fields & Bitwise Operations

  11. “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 Bit Fields & Bitwise Operations

  12. structstatusReg {unsigned intemptyPaperTray :1;unsigned intpaperJam :1; :2; unsigned intlowInk :1; :1;unsigned intneedsCleaning :1; :1; }; Clean Low ink Paper jam Empty paper Modern Bit-field Definitions Bit Fields & Bitwise Operations

  13. structstatusReg {unsigned intemptyPaperTray :1;unsigned intpaperJam :1; :1;unsigned int count :5; :1;unsigned intlowInk :1; :1;unsigned intneedsCleaning :1; :1; }; count Clean Low ink Paper jam Empty paper Printer Status Register (cont.) Bit Fields & Bitwise Operations

  14. Modern Bit-fields (continued) structstatusReg s; if (s.empty && s.jam) ...; while(! s.lowInk) ...; s.needsCleaning = true; s.Jam = false; int c = s.count; s.count -= 1; Bit Fields & Bitwise Operations

  15. Questions about Bit Fields? Bit Fields & Bitwise Operations

More Related