1 / 46

Bit-Level vs Logical Operators in Computer Systems

This article reviews the concepts of bit-level vs logical operators in computer systems, including addition using 2's complement, sign extension, memory and variables, bitwise operators, and bitmasks.

stamper
Download Presentation

Bit-Level vs Logical Operators in Computer Systems

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. CSC 2400: Computer SystemsBit-Level vs. Logical Operators

  2. Review – Addition • 2’s complement addition is just binary addition • assume all integers have the same number of bits • for now, assume no overflow 01101000 (104) 11110110 (-10) + 11110000(-16) + (-9) 01011000 (88) (-19) Assume 8-bit 2’s complement numbers.

  3. Review – Sign Extension • To add two numbers, we must represent them with the same number of bits. • If we just pad with zeroes on the left NOT GOOD!): • Instead, replicate the sign bit to the left: 4-bit8-bit 0100 (4)00000100 (still 4) 1100 (-4)00001100 (12, not -4; NOT GOOD!) 4-bit8-bit 0100 (4)00000100 (still 4) 1100 (-4)11111100 (still -4)

  4. Review – Memory and Variables char c = ‘A’; char c = 65; char c = 0x41; int i = 258; int i = 0x102; int j = 0xF0A2;

  5. Bit-Level OperatorsCommon to C and Java&, |, ~, ^, <<, >>

  6. Bitwise Operators • Apply to any “integral” data type: long, int, short, char • View arguments as bit vectors • Arguments applied bit-wise

  7. Bitwise Operators

  8. NOT (~)Example

  9. AND (&) and OR (I)and XOR (^) Examples

  10. AND (&) and OR (I)and XOR (^) Examples 0x69 & 0x55 --> 0x______ 0x69 | 0x55 --> 0x______ 0x69 ^ 0x55 --> 0x______

  11. Bitmasks • Used to change or query one or more bits in a variable • The bitmask indicates which bits are to be affected • Common operations: • Setting one or more bits to 1 • Setting one or more bits to 0 • Reading one or more bits

  12. Bitmasks – set single bit to 1 • Assume that data is an integer variable (size is unknown) • Set the least significant bit of data to 1 (other bits are unchanged) • C statement: data | 1 data = _______________________;

  13. Bitmasks – set multiple bits to 1 • Assume that data is an integer variable (size is unknown) • Set bit 0, 2 and 3 of data to 1 (leave the other bits unchanged): • C statement: data | 0xD data = _______________________;

  14. Bitmasks – set byte to 0xFF • Assume that data is an integer variable (size is unknown) • Set the least significant byte of data to 0xFF • C statement: data | 0xFF data = _______________________;

  15. Bitmasks – set another byte to 0xFF • Assume that data is an integer variable (size is unknown) • Set the 2nd least significant byte of data to 0xFF • C statement: data | 0xFF00 data = _______________________;

  16. Bitmasks – clear (set to 0) single bit • Assume that data is an integer variable (size is unknown) • Set the least significant bit of data to 0: • C statement: data & ~1 data = _______________________;

  17. Review – Common Bit Operations • Set one or more bits to 1 • Set one or more bits to 0 • Read one or more bits

  18. Review – What Does This Code Do? int data = ...; data = data | 0x1; • Answer: • Set the least significant bit to 1

  19. Review – What Does This Code Do? int data = ...; data = data | 0x4; bit 2 bit 1 bit 0 • Answer: • Set bit 2 (third least significant bit) to 1

  20. Review – What Does This Code Do? int data = ...; data = data | 0xF; bit 3 bit 2 bit 1 bit 0 • Answer: • Set the four least significant bits to 1

  21. Review – What Does This Code Do? int data = ...; data = data & ~0x1; bit 0 • Answer: • Clear the least significant bit (set it to 0)

  22. Bitmasks – clear (set to 0) multiple bits • Assume that data is an integer variable (size is unknown) • Clear (set to 0) bits 0, 2 and 4 of data (other bits are unchanged): • C statement: data & ~0x15 data = _______________________;

  23. Bitmasks – clear (set to 0) single byte • Assume that data is an integer variable (size is unknown) • Clear (set to 0) the least significant byte of data • C statement: data & ~0xFF data = _______________________;

  24. Feedback Results • Review Topics (Today) • Ungraded Practice Quizzes (1/week) • Review Homework • More Hands-on/Labs/C Programming (Coming Up) • Teaching • Slow Down • Explain Jargon • Step-by-step Examples • When post slides?

  25. Bitmasks – clear (set to 0) multiple bytes • Assume that data is an integer variable (size is unknown) • Clear (set to 0) bytes 0 and 1 of data • C statement: data & ~0xFFFF data = _______________________;

  26. Bitmasks – read bit • Assume that data is an integer variable (size is unknown) • Determine the least significant bit of data: • C statement: data & 1 result = _______________________;

  27. Bitmasks – read another bit • Assume that data is an integer variable (size is unknown) • Determine the 2nd least significant bit of data: • Need to shift

  28. Bitwise Operator: Left Shift << • Left shift: A << n (same as multiply by 2n) • Shift the bit-vector A to the left by n bits • Leftmost n bits are lost; rightmost n bits become 0

  29. Bitwise Operator: Right Shift >> • Right shift: A >> n (same as divide by 2n) • Shift the bit-vector A to the right by n bits • Rightmost n bits are lost; what about leftmost n bits? 1 1 1 0 0 0 • Uses sign extension for signed types

  30. Bitwise Operator: Right Shift >> 0 0 0 0 0 0 • Insert 0 to the left for unsigned types Note: Java has the unsigned shift operator >>> (not available in C)

  31. Bitmasks – revisit example • Determine the 2nd least significant bit of data (size unknown): • C statement: (data >> 1) & 1 result = _______________________;

  32. Bitmasks – read another bit • Determine the 4nd least significant bit of data (size unknown): • C statement: (data >> 4) & 1 result = _______________________;

  33. Bitmasks – read byte • Assume that data is an integer variable (size is unknown) • Determine the 2nd least significant byte of data • C statement: (data >> 8) & 0xFF result = _______________________;

  34. Bitwise Operators • Write code to print all the bits of a character from left to right (starting with the most significant bit): char c = 0xB5; void print_binary(char c) { /* add code here */ } 1 0 1 1 0 1 0 1

  35. Relations Between Operations • DeMorgan’s Laws • Express & in terms of |, and vice-versa • A & B = ~(~A | ~B) • A and B are true if and only if neither A nor B is false • A | B = ~(~A & ~B) • A or B are true if and only if A and B are not both false • Exclusive-Or using Inclusive Or • A ^ B = (~A & B) | (A & ~B) • Exactly one of A and B is true • A ^ B = (A | B) & ~(A & B) • Either A is true, or B is true, but not both

  36. Why Bitwise Operators?

  37. Used in Networking

  38. Used in Encryption/ Decryption

  39. Used in Compression • Look at the DEFLATE algorithm, for instance • https://en.wikipedia.org/wiki/DEFLATE • everything is bits, not bytes

  40. Review – Bitwise Operators ~ & | ^ << >>

  41. Contrast: Logical Operators

  42. Logical Operators • Always return 0 or 1 • False is 0 in C • True is anything nonzero in C • Examples (char data type) • !0x41 --> _____ • !0x00 --> _____ • !!0x41 --> _____ • 0x69 && 0x55 --> _____ • 0x69 || 0x55 --> _____ && (Logical AND), || (Logical OR), ! (Logical NOT)

  43. What is the Output? int a = 0x43, b = 0x21; printf("a | b = %x\n", a | b); _________ Printf("a || b = %x\n", a || b); ____1____ printf("a & b = %x\n", a & b); _________ printf("a && b = %x\n", a && b); ____1____

  44. Mental Exercise /* * isNotEqual - return 0 if x == y, and 1 otherwise * Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1 */ intisNotEqual (int x, int y) { return ______________; }

  45. What did we learn? • Bit-level operators (&, |, ~, ^, <<, >>) • Operate on bits (view arguments as bit vectors) • Logical operators (&&, ||, !) • Always return 0 (False) or 1 (True)

  46. Midterm Review Topics • Number Representations and Casting/Conversion: • Binary, Hexadecimal, Octal, Little and big endian, sign extension • Signed Magnitude, 1’s Complement, 2’s Complement • IEEE 32-bit and 64-bit Floating Point, ASCII • Addition in different representations, overflow, limits of representation • Bitwise and Logical Operators: AND, OR, NOT, XOR, L&R SHIFT • Unix Commands: ls, cd, mkdir, cp, mv, rm, chmod, pwd, -r, etc • Absolute and Relative Paths, permissions, navigating directory structure • C programming and differences with Java • Computer Systems (hw/sw, analog/digital), Data Sizes, etc • Sources: ZyBook, Tarnoff, Slides, Labs and Homework • Problems: Short Answer, MC, True False, and Problem Solving

More Related