Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 10 Operations on Bits

Similar presentations


Presentation on theme: "Module 10 Operations on Bits"— Presentation transcript:

1 Module 10 Operations on Bits

2 @UMBC Training Centers 2012
What’s 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) @UMBC Training Centers 2012

3 @UMBC Training Centers 2012
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 = = = 3810 (in binary!!) 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 @UMBC Training Centers 2012

4 @UMBC Training Centers 2012
Binary Exercises ex. 1 = 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @UMBC Training Centers 2012

5 Binary Exercises – Round 2
= 117 1 86 2 158 3 201 4 136 5 250 6 179 7 23 8 100 9 200 10 231 @UMBC Training Centers 2012

6 Another way of converting a Decimal to Binary
Another way of Solving Routinely done in programming 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 = 68 (base 10) @UMBC Training Centers 2012

7 @UMBC Training Centers 2012
Octal Octal = base 8 A group of 3 bits is one octal digit Octal constants start with 0 Binary Octal Decimal 000 001 1 010 2 011 3 100 4 101 5 110 6 111 7 @UMBC Training Centers 2012

8 @UMBC Training Centers 2012
Octal to Decimal @UMBC Training Centers 2012

9 @UMBC Training Centers 2012
Another way of Solving @UMBC Training Centers 2012

10 @UMBC Training Centers 2012
Octal in Applications @UMBC Training Centers 2012

11 @UMBC Training Centers 2012
Exercises @UMBC Training Centers 2012

12 @UMBC Training Centers 2012
Hexadecimal Hexadecimal = base 16 A group of 4 bits is one hex digit Hex constants start with 0x Binary Hex Decimal 0000 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 Binary Hex Decimal 1000 8 1001 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 @UMBC Training Centers 2012

13 Hexadecimal to Decimal
@UMBC Training Centers 2012

14 Hexadecimal in an Application
@UMBC Training Centers 2012

15 @UMBC Training Centers 2012
Another way of Solving @UMBC Training Centers 2012

16 @UMBC Training Centers 2012
Exercises @UMBC Training Centers 2012

17 Why Binary , Octal and Hex?
Translate the following 32-bit integer Octal Hexadecimal A D A 5 @UMBC Training Centers 2012

18 @UMBC Training Centers 2012
Two’s 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 = Then complement = Then add 1, = @UMBC Training Centers 2012

19 Two’s Compliment Visually
@UMBC Training Centers 2012

20 Solving for Two’s Compliment
@UMBC Training Centers 2012

21 @UMBC Training Centers 2012
More two’s complement Largest value in a byte: = 127 Smallest value in a byte: = -128 -1 in a byte = Smallest value in a 4-byte (32-bit) integer -232 – 1 = -2,147,483,648 Largest value in 4-byte (32 bit) integer 231 – 1 = 2, 147, 483, 647 @UMBC Training Centers 2012

22 @UMBC Training Centers 2012
Exercises Integer Representation Exercises Submit it to your “submission” folder @UMBC Training Centers 2012

23 @UMBC Training Centers 2012
Bitwise Operators Apply to any integer type or to characters Result is 0 or 1 Apply to an integer bit-by-bit @UMBC Training Centers 2012

24 @UMBC Training Centers 2012
Bitwise AND & (AND) short w1 = 25, w2 = 77; short w3 = w1 & w2; w1 = w2 = w3 = = 9 A B A & B 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 @UMBC Training Centers 2012

25 @UMBC Training Centers 2012
Masking Using the AND operator to select bits. char c = 45; c = c & 0x7; //0x means Octal c = 0x7 = c & 0x7 = @UMBC Training Centers 2012

26 @UMBC Training Centers 2012
Let’s Take a Look C::B Program 12-1 GUESS the output FIRST Then go ahead and run to confirm your answer @UMBC Training Centers 2012

27 @UMBC Training Centers 2012
Bitwise OR | (OR) short w1 = 0431, w2 = 0152; short w3 = w1 | w2; w1 = w2 = w3 = = 0573 A B A | B 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 @UMBC Training Centers 2012

28 @UMBC Training Centers 2012
Turning Bits “On” Bits may be turned “on” (set to 1) by ORing short w1 = 0431; w1 = w1 | 07; w1 = 07 = w1 | 7 = @UMBC Training Centers 2012

29 Bitwise XOR ^ (XOR) short w1 = 0536, w2 = 0266; short w3 = w1 ^ w2; w1 = w2 = w3 = = 0750 A B A ^ B 1 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 UMBC Training Centers, LLC @UMBC Training Centers 2012 29

30 @UMBC Training Centers 2012
Bitwise Complement ~ (1’s complement) short w1 = ; short w2 = ~w1; w1 = = w2 = = A ~A 1 Notes: ~0 = -1 regardless of the number of bits in an int @UMBC Training Centers 2012

31 @UMBC Training Centers 2012
Let’s 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 @UMBC Training Centers 2012

32 @UMBC Training Centers 2012
Exercise PP2-14.docx Complete Submit in your “submit” folder @UMBC Training Centers 2012

33 @UMBC Training Centers 2012
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 = d = @UMBC Training Centers 2012

34 @UMBC Training Centers 2012
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 @UMBC Training Centers 2012

35 @UMBC Training Centers 2012
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. @UMBC Training Centers 2012

36 @UMBC Training Centers 2012
Signed Right-Shift signed short w1 = 0x9013, w2 = 0x0322; signed short w3 = w1 >> 3; signed short w4 = w2 >> 2; w1 = w3 = w2 = w4 = @UMBC Training Centers 2012

37 @UMBC Training Centers 2012
Notes on Shifting Right-shift is used to divide by 2N Shifting with a negative count is undefined @UMBC Training Centers 2012

38 @UMBC Training Centers 2012
Unsigned Right-Shift unsigned short w1 = 0x9013, w2 = 0x0322; unsigned short w3 = w1 >> 3; unsigned short w4 = w2 >> 2; w1 = w3 = w2 = w4 = @UMBC Training Centers 2012

39 @UMBC Training Centers 2012
Let’s take a look C::B Program 12-3 @UMBC Training Centers 2012

40 @UMBC Training Centers 2012
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 @UMBC Training Centers 2012

41 @UMBC Training Centers 2012
Rotate Examples unsigned short w1 = 0x4356; unsigned short w2 = rightRotate(w1, 3); Unsigned short w3 = 0x9234; unsigned short w4 = leftRotate(w1, 2); w1 = w2 = w3 = w4 = @UMBC Training Centers 2012

42 @UMBC Training Centers 2012
Let’s Take a Look C::B – Program 12-4 Notice that shift operators have higher precedence than bit-wise ops Would prefer separate functions @UMBC Training Centers 2012

43 @UMBC Training Centers 2012
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 day month 1 - 12 year 0 -99 @UMBC Training Centers 2012

44 @UMBC Training Centers 2012
March 11, 2033 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 day month 1 - 12 year 0 -99 1 15 14 13 12 11 10 9 8 7 6 5 4 3 2 day = 11 month = 3 year = 33 @UMBC Training Centers 2012

45 Extracting Day from the Byte
int ymd = 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 1 1 1 1 1 1 1 year = 33 month = 3 day = 11 0x1F = 1 1 1 1 1 ymd & 0x1F = 1 1 1 day = ymd & 0x1F; @UMBC Training Centers 2012

46 @UMBC Training Centers 2012
Extracting Month 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 int ymd = 1 1 1 1 1 1 1 year = 33 month = 3 day = 11 0x1E0 = 1 1 1 1 ymd & 0x1E0 = 1 1 ymd & 0x1E0 >> 5 = 1 1 month = ymd & 0x1E0 >> 5; @UMBC Training Centers 2012

47 @UMBC Training Centers 2012
Extracting Year 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ymd = 1 1 1 1 1 1 1 year = 33 month = 3 day = 11 0x7E00 = 1 1 1 1 1 1 1 1 ymd & 0x7E00 = ymd & 0x7E00 >> 9 = 1 1 year = ymd & 0x7E00 >> 9; @UMBC Training Centers 2012

48 Packing Bits short ymd = (year << 9) | (month << 5) | day;
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 // March 11, 2033 short day = 11; short month = 3; short year = 33; 1 1 1 1 1 1 day = 11 1 1 month = 3 1 1 year = 33 1 1 1 1 1 1 1 1 1 year = 33 month=3 day = 11 short ymd = (year << 9) | (month << 5) | day; @UMBC Training Centers 2012

49 @UMBC Training Centers 2012
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); @UMBC Training Centers 2012

50 @UMBC Training Centers 2012
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 @UMBC Training Centers 2012

51 @UMBC Training Centers 2012
If you have any comments about this video, please use the contact information in your registration materials to let us know. @UMBC Training Centers 2012


Download ppt "Module 10 Operations on Bits"

Similar presentations


Ads by Google