Bits and Bytes in Computer Memory
Computer memory is composed of bits representing 0 or 1, grouped into bytes. Learn about binary operations, converting decimal to binary, octal representation, and exercises in this module. Understand how individual bits in a byte correlate to powers of 2 and perform binary exercises to strengthen your understanding. Explore the conversion process from decimal to binary and the concept of octal numbers in computer systems.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Module 10 Module 10 Operations on Bits Operations on Bits
Whats a bit/byte? Computer memory is made up of hardware that can represent a 0 or a 1. Each individual 0 or 1 is a bit (Binary digIT) C provides operators for manipulating these individual bits Bits are commonly group in sets of 8 known as a byte Bits in a byte are numbered from right-to-left starting with bit 0 Bit 0 is known as the least significant bit (lsb) www.umbctraining.com @UMBC Training Centers 2012 2
Bits and Integers Each bit in a byte represents a power of 2 Bit 0 represents 20 = 1 Bit 1 represents 21 = 2 etc Example: What s the value of 001001102 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 0 0 1 0 0 1 1 0 001001102 = 25 + 22 + 21 = 32 + 4 + 2 = 3810 (in binary!!) www.umbctraining.com @UMBC Training Centers 2012 3
Binary Exercises 0 0 1 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 1 1 1 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 0 1 1 1 1 0 0 1 1 0 0 1 0 1 0 1 0 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 1 1 1 0 0 0 1 0 0 0 0 1 0 1 1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 1 1 1 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 0 = = = = = = = = = = = = = = = = = = ex. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 www.umbctraining.com @UMBC Training Centers 2012 4
Binary Exercises Round 2 = = = = = = = = = = = 117 86 158 201 136 250 179 23 100 200 231 Ex. 1 2 3 4 5 6 7 8 9 10 www.umbctraining.com @UMBC Training Centers 2012 5
Another way of Solving Another way of converting a Decimal to Binary 68 2 = 34 R 0 34 2 = 17 R 0 17 2 = 8 R 1 8 2 = 4 R 0 4 2 = 2 R 0 2 2 = 1 R 0 1 2 = 0 R 1 1 0 0 0 1 0 0 = 68 (base 10) Routinely done in programming www.umbctraining.com @UMBC Training Centers 2012 6
Octal Octal = base 8 A group of 3 bits is one octal digit Octal constants start with 0 Binary Octal Decimal 000 0 0 001 1 1 010 2 2 011 3 3 100 4 4 101 5 5 110 6 6 111 7 7 www.umbctraining.com @UMBC Training Centers 2012 7
Octal to Decimal www.umbctraining.com @UMBC Training Centers 2012 8
Another way of Solving www.umbctraining.com @UMBC Training Centers 2012 9
Octal in Applications www.umbctraining.com @UMBC Training Centers 2012 10
Exercises www.umbctraining.com @UMBC Training Centers 2012 11
Hexadecimal Hexadecimal = base 16 A group of 4 bits is one hex digit Hex constants start with 0x Binary Hex Decimal Binary Hex Decimal 0000 0 0 1000 8 8 0001 1 1 1001 9 9 0010 2 2 1010 A 10 0011 3 3 1011 B 11 0100 4 4 1100 C 12 0101 5 5 1101 D 13 0110 6 6 1110 E 14 0111 7 7 1111 F 15 www.umbctraining.com @UMBC Training Centers 2012 12
Hexadecimal to Decimal www.umbctraining.com @UMBC Training Centers 2012 13
Hexadecimal in an Application www.umbctraining.com @UMBC Training Centers 2012 14
Another way of Solving www.umbctraining.com @UMBC Training Centers 2012 15
Exercises www.umbctraining.com @UMBC Training Centers 2012 16
Why Binary , Octal and Hex? Translate the following 32-bit integer 00101010011101101000110110100101 Octal 000 101 010 011 101 101 000 110 110 100 101 0 5 2 3 5 5 0 6 6 4 5 Hexadecimal 0010 1010 0111 0110 1000 1101 1010 0101 2 A 7 6 8 D A 5 www.umbctraining.com @UMBC Training Centers 2012 17
Twos Complement To store both positive and negative values, the left-most bit is the designated sign bit Sign bit = 0 indicates a positive value Sign bit = 1 indicates a negative value To store a negative number, store its absolute value, add 1, then complement the result. To store 5, first store 5 = 00000101 Then complement = 11111010 Then add 1, = 11111011 www.umbctraining.com @UMBC Training Centers 2012 18
Twos Compliment Visually www.umbctraining.com @UMBC Training Centers 2012 19
Solving for Twos Compliment www.umbctraining.com @UMBC Training Centers 2012 20
More twos complement Largest value in a byte: 01111111 = 127 Smallest value in a byte: 10000000 = -128 -1 in a byte = 11111111 Smallest value in a 4-byte (32-bit) integer 10000000000000000000000000000000 -232 1 = -2,147,483,648 Largest value in 4-byte (32 bit) integer 01111111111111111111111111111111 231 1 = 2, 147, 483, 647 www.umbctraining.com @UMBC Training Centers 2012 21
Exercises Integer Representation Exercises Submit it to your submission folder www.umbctraining.com @UMBC Training Centers 2012 22
Bitwise Operators Apply to any integer type or to characters Result is 0 or 1 Apply to an integer bit-by-bit www.umbctraining.com @UMBC Training Centers 2012 23
Bitwise AND & (AND) short w1 = 25, w2 = 77; short w3 = w1 & w2; A B A & B 0 0 0 w1 = 0000000000011001 w2 = 0000000001001101 ---------------------- w3 = 0000000000001001 = 9 0 1 0 1 0 0 1 1 1 Notes: a & 1 = a ANDing a bit with 1 retains the value of the bit a & 0 = 0 ANDing a bit with 0 sets the bit to 0 www.umbctraining.com @UMBC Training Centers 2012 24
Masking Using the AND operator to select bits. char c = 45; c = c & 0x7; //0x means Octal c = 01001101 0x7 = 00000111 c & 0x7 = 00000101 www.umbctraining.com @UMBC Training Centers 2012 25
Lets Take a Look C::B Program 12-1 GUESS the output FIRST Then go ahead and run to confirm your answer www.umbctraining.com @UMBC Training Centers 2012 26
Bitwise OR | (OR) short w1 = 0431, w2 = 0152; short w3 = w1 | w2; A B A | B 0 0 0 w1 = 100 011 001 w2 = 001 101 010 ----------------- w3 = 101 111 011 = 0573 0 1 1 1 0 1 1 1 1 Notes: a | 1 = 1 ORing a bit with 1 sets the bit to 1 a | 0 = a ORing a bit with 0 retains the value of the bit www.umbctraining.com @UMBC Training Centers 2012 27
Turning Bits On Bits may be turned on (set to 1) by ORing short w1 = 0431; w1 = w1 | 07; w1 = 100 011 001 07 = 000 000 111 w1 | 7 = 100 011 111 www.umbctraining.com @UMBC Training Centers 2012 28
Bitwise XOR ^ (XOR) short w1 = 0536, w2 = 0266; short w3 = w1 ^ w2; A B A ^ B 0 0 0 w1 = 101 011 110 w2 = 010 110 110 ----------------- w3 = 111 101 000 = 0750 0 1 1 1 0 1 1 1 0 Notes: 1 ^ 1 = 0 and 0 ^ 1 = 1 XORing a bit with 1 changes the bit 0 ^ 0 = 0 and 1 ^ 0 = 1 XORing a bit with 0 retains the value of the bit www.umbctraining.com www.umbctraining.com @UMBC Training Centers 2012 UMBC Training Centers, LLC 29 29
Bitwise Complement ~ (1 s complement) short w1 = 0122457; short w2 = ~w1; A ~A 0 1 w1 = 001 010 010 100 101 111 = 0122457 w2 = 110 101 101 011 010 000 = 0055320 1 0 Notes: ~0 = -1 regardless of the number of bits in an int www.umbctraining.com @UMBC Training Centers 2012 30
Lets Take a Look C::B- Program12-2 Predict output first, run it to confirm Note: warnings from line 15 suggest parens Change to use parens around 1st two operands different results! Note precedence of bitwise operators Pg 440 -> Kochan book Redo parens around last 2 operand to get original results3 www.umbctraining.com @UMBC Training Centers 2012 31
Exercise PP2-14.docx Complete Submit in your submit folder www.umbctraining.com @UMBC Training Centers 2012 32
Left-Shift Operator The << (left shift) operator literally moves bits in a value to the left. The left-most (high- order) bits are lost and 0 s replace the right most (low-order) bits which are shifted. char c = 0x43; char d = c << 2; c = 0100 0011 d = 0000 1100 www.umbctraining.com @UMBC Training Centers 2012 33
Notes on Shifting Left-shift is used to multiply by 2N Shifting with a count that is more than 32 bits (number of bits in an int) is undefined www.umbctraining.com @UMBC Training Centers 2012 34
Right-shift Operator The >> (right shift) operator literally moves bits in a value to the right. The right-most (low-order) bits are lost. If the value is signed (the default) then copies of the sign-bit fill in the high-order bits. If the value is unsigned , then 0s fill in the high-order bits. www.umbctraining.com @UMBC Training Centers 2012 35
Signed Right-Shift signed short w1 = 0x9013, w2 = 0x0322; signed short w3 = w1 >> 3; signed short w4 = w2 >> 2; w1 = 1001 0000 0001 0011 w3 = 1111 0010 0000 0010 w2 = 0000 0011 0010 0010 w4 = 0000 0000 1100 0100 www.umbctraining.com @UMBC Training Centers 2012 36
Notes on Shifting Right-shift is used to divide by 2N Shifting with a negative count is undefined www.umbctraining.com @UMBC Training Centers 2012 37
Unsigned Right-Shift unsigned short w1 = 0x9013, w2 = 0x0322; unsigned short w3 = w1 >> 3; unsigned short w4 = w2 >> 2; w1 = 1001 0000 0001 0011 w3 = 0001 0010 0000 0010 w2 = 0000 0011 0010 0010 w4 = 0000 0000 1100 0100 www.umbctraining.com @UMBC Training Centers 2012 38
Lets take a look C::B Program 12-3 www.umbctraining.com @UMBC Training Centers 2012 39
Rotating Bits No rotate operator Need to write a function Similar to shifting Left-Rotate High-order bits that were lost when shifting become the low-order bits Right-Rotate Low-order bits that were lost when shifting become the high-order bits www.umbctraining.com @UMBC Training Centers 2012 40
Rotate Examples unsigned short w1 = 0x4356; unsigned short w2 = rightRotate(w1, 3); Unsigned short w3 = 0x9234; unsigned short w4 = leftRotate(w1, 2); w1 = 0100 0011 0101 0110 w2 = 1100 1000 0110 1010 w3 = 1001 0010 0011 0100 w4 = 0100 1000 1101 0010 www.umbctraining.com @UMBC Training Centers 2012 41
Lets Take a Look C::B Program 12-4 Notice that shift operators have higher precedence than bit-wise ops Would prefer separate functions www.umbctraining.com @UMBC Training Centers 2012 42
Bit Fields Multiple data values can be stored in an int, short Values are set and extracted via mask & shift 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 day 1 - 31 year 0 -99 month 1 - 12 www.umbctraining.com @UMBC Training Centers 2012 43
March 11, 2033 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 day 1 - 31 year 0 -99 month 1 - 12 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 day = 11 year = 33 month = 3 www.umbctraining.com @UMBC Training Centers 2012 44
Extracting Day from the Byte 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 int ymd = 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 day = 11 year = 33 month = 3 0x1F = 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 ymd & 0x1F = 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 day = ymd & 0x1F; www.umbctraining.com @UMBC Training Centers 2012 45
Extracting Month 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 int ymd = 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 day = 11 year = 33 month = 3 0x1E0 = 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 ymd & 0x1E0 = 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 ymd & 0x1E0 >> 5 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 month = ymd & 0x1E0 >> 5; www.umbctraining.com @UMBC Training Centers 2012 46
Extracting Year 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 ymd = day = 11 year = 33 month = 3 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0x7E00 = ymd & 0x7E00 = 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 ymd & 0x7E00 >> 9 = 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 year = ymd & 0x7E00 >> 9; www.umbctraining.com @UMBC Training Centers 2012 47
Packing Bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // March 11, 2033 short day = 11; short month = 3; short year = 33; 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 day = 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 month = 3 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 year = 33 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 0 1 1 day = 11 year = 33 month=3 shortymd = (year << 9) | (month << 5) | day; www.umbctraining.com @UMBC Training Centers 2012 48
Struct Bit Fields struct ymd { unsigned int year:6; unsigned int month:4; unsigned int day: 5; }; struct ymd birthday; // in main() Access fields using dot notation Fields automatically converted to integers birthday.year = 44; printf( %d\n , birthday.year); www.umbctraining.com @UMBC Training Centers 2012 49
Exercises Text (page 297) #1 (12.1, 12.2, 12.3 in HEX, 12.4 in octal) # Text #5 + 7 Ex12-5_7.docx bitTest and bitSet 2 lines of code bigGet - extract bits from an unsigned int. e.g. extractBits( x, 3, 5) extracts bits 3-7 bit is lsb Ex12-6.docx bitPatternMatch( x, pattern, n ) are the rightmost N bits of pattern in x Change bit orientation so that bit 0 is lsb Assume 32-bit integers Ex1-Bytes.docx -Extract byte N Challenge - replace value in byte N Ex2-Fields.docx breaking apart packed data www.umbctraining.com @UMBC Training Centers 2012 50