Presentation is loading. Please wait.

Presentation is loading. Please wait.

Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download

Similar presentations


Presentation on theme: "Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download"— Presentation transcript:

1 Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download http://ebooks.edhole.com

2 Bit Fields & Bitwise Operations CS-2301, B-Term 20092 Bit Fields & Bitwise Operations CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) http://ebooks.edhole.com

3 Bit Fields & Bitwise Operations CS-2301, B-Term 20093 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 http://ebooks.edhole.com

4 Bit Fields & Bitwise Operations CS-2301, B-Term 20094 Review – Bitwise Operations in Integers & – 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 Corresponding bits of both operands are combined by the usual logic operations. Apply to all kinds of integer types:– Signed and unsigned char, short, int, long, long long http://ebooks.edhole.com

5 Bit Fields & Bitwise Operations CS-2301, B-Term 20095 Examples unsigned int c, a, b; c = a & b; c = a | b; c = a ^ b; c = ~a; c = a << 2; c = a >> 3; 11110000 a 10101010 b http://ebooks.edhole.com

6 Bit Fields & Bitwise Operations CS-2301, B-Term 20096 Right Shift is Tricky unsigned int c, a; c = a >> 3; signed int c, a, b; c = b >> 3; c = a >> 3; 11110000 a 1111000011110000 a 1111000001010101 b 01010101 http://ebooks.edhole.com

7 Bit Fields & Bitwise Operations CS-2301, B-Term 20097 Two Approaches Traditional C Use #define and a lot of bitwise operations Modern Use bit fields http://ebooks.edhole.com

8 Bit Fields & Bitwise Operations CS-2301, B-Term 20098 Example – Printer Status Register Traditional C definition of bit fields #define EMPTY 01 #define JAM 02 #define LOW_INK 16 #define CLEAN 64 Empty paper Paper jam Low ink Clean http://ebooks.edhole.com

9 Bit Fields & Bitwise Operations CS-2301, B-Term 20099 Example – Printer Status Register (cont.) 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 */ Empty paper Paper jam Low ink Clean http://ebooks.edhole.com

10 Bit Fields & Bitwise Operations CS-2301, B-Term 200910 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 http://ebooks.edhole.com

11 Bit Fields & Bitwise Operations CS-2301, B-Term 200911 Example – Printer Status Register (cont.) An integer field (traditional style) #define COUNT (8|16|32|64|128) int c = (status & COUNT) >> 3; status |= (c << 3) & COUNT; Empty paper Paper jam Low ink Clean count http://ebooks.edhole.com

12 Bit Fields & Bitwise Operations CS-2301, B-Term 200912 “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 http://ebooks.edhole.com

13 Bit Fields & Bitwise Operations CS-2301, B-Term 200913 Modern Bit-field Definitions struct statusReg { unsigned int emptyPaperTray :1; unsigned int paperJam :1; :2; unsigned int lowInk :1; :1; unsigned int needsCleaning :1; :1; }; Empty paper Paper jam Low ink Clean http://ebooks.edhole.com

14 Bit Fields & Bitwise Operations CS-2301, B-Term 200914 Example – Printer Status Register (cont.) 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; }; Empty paper Paper jam Low ink Clean count http://ebooks.edhole.com

15 Bit Fields & Bitwise Operations CS-2301, B-Term 200915 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; http://ebooks.edhole.com

16 Bit Fields & Bitwise Operations CS-2301, B-Term 200916 Questions about Bit Fields? http://ebooks.edhole.com

17 Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download http://ebooks.edhole.com


Download ppt "Free Ebooks Download Mba Ebooks By Edhole Mba ebooks Free ebooks download"

Similar presentations


Ads by Google