Lecture 2: Operations Bryan Burlingame 9Sept2015
Announcements Homework posted Homework 1 due next week Bring headphones to lab Still do not have Canvas access For Lab, turn in a hard copy
Recall binary Base 2 number system Mimics the actual hardware computers are built upon Two values, 0 and to decimal = 120
Binary to Hexadecimal Hexadecimal (base 16) is a convenient numbering system 0 – 9, A – F (A = 10 10, B = 11 10, etc Observe 2 4 = 16, allows four bits (a nyble) to be grouped = = 0xC 16
Binary to Hexadecimal Convert Binary to Hex b
Binary to Hexadecimal Convert Binary to Hex b (grouping 4 bits) d C 3 9 F h
Hex to Decimal C39Fh 12 * * * * 16 0 = 50,079 CharacterValue F15 E14 D13 C12 B11 A10 0 –
Decimal to binary Many ways, this is how I do it 120 120 < 128, 0 in 7 th position 120 > 64, 1 in 6 th position 120 – 64 = 56 56 > 32, 1 in 5 th position 56 – 32 = 24 24 > 16, 1 in 4 th position 24 – 16 = 8 8 = 8, 1 in 3 rd position 8 – 8 = 0 Once we hit zero, all other values are zero Why do I do it this way? It is quick when I don’t have a calculator. Your mileage may vary BinaryDecimal
Representations Decimal (default)BinaryHexadecimal 1080’ x6C 108d b6Ch C dec bin6C hex Group by 3s with a comma Group by 4s with a space or an underbar Generally don’t group Spoken short form
Bitwise arithmetic Mimics digital hardware Based on Boolean logic Operates on each bit within a number (i.e. operates bitwise) Many, many hardware peripherals take advantage of bitwise operations Implemented directly in hardware Very fast
Binary (Bitwise) And - & & Y X 194 & 225 = (194) & (225) (192) Binary And is commonly used with “bitmasks” A binary & used with a bitmask can ensure that a given bit is turned “off” & is shift + 7
Binary (Bitwise) Or - | | Y X 194 | 225 = (194) | (225) (227) Binary OR, is, also, commonly used with “bitmasks” A binary | used with a bitmask can ensure that a given bit is turned “on” | is shift + \
Binary (Bitwise) Xor - ⊕ ⊕ Y X 194 ^ 225 = (194) ⊕ (225) (35) Xors are commonly used to switch the values of selected bits or to test for inequivalence C uses ^ for Xor ^ is shift + 6
Bitshifts ( > Right Shift) A bit shift shifts bits in a direction Left shift shifts bits to the left and simulates a multiply of 2 Right shift shifts bits to the right and simulates an integer division of 2 Bits outside the range are lost 23 << 3 = 184 (left shift by 3) shifted = >> 2 = 5 (right shift by 2) = 5 (note the last two bits are lost)
True and False Generally, false is zero and true is not- zero Usually, all comparisons which are true generate a 1 (23 > 4) = 1 This comes up a lot!
Modulo Division (%) Integer division which provides the “remainder” from an integer division Assuming integer operations 123 % 6 = 3 123 / 6 = 20 (note, the fractional portion will be dropped) Recall: False is 0, and True is not 0 What does X % 2 give for all odd numbers?
Order of operations – C & Matlab Operators * / %Multiplication, division, modulo (integer) division + -Addition, subtraction >Left bitshift, right bitshift &Bitwise and ^Bitwise xor |Bitwise or
References Nicholas, P. (1994). Digital Design. Minneapolis/St. Paul: West Publishing Company