Download presentation
Presentation is loading. Please wait.
Published byMelina Underwood Modified over 6 years ago
1
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings
2
Operators – Left Shift <<
The Bitwise Left Shift (<<) operator takes two operands, the second of which must be an integer. char result = value << 2; The << takes the first number's binary representation and gives a result whose binary representation is shifted over by the second operand number of bits. The leftmost bits simply disappear and the new rightmost bits are 0. In the example above, we do a left shift by 2. value result
3
Operators – Right Shift >>
Like the Left Shift, the right shift (>>) operator takes two operands, the second of which must be an integer. char result = value >> 3; The >> takes the first number's binary representation and gives a result whose binary representation is shifted over by the second operand number of bits. For signed values, the leftmost bits are extended from the original sign. For unsigned values, they're 0. Signed value result unsigned value result
4
Shifting – Arithmetic Equivalence
Shifting a value by a number of bits has the same impact as multiplying or dividing by a power of 2. x << n == x * 2n+1 x >> n == x / 2n+1 The consequence of this is that any time you would want to multiply by a power of two, you don't need to calculate the exponent. This operation has many uses in optimization as a result. The power function in most rudimentary math libraries is a looping algorithm. The shift operators are single CPU instructions. Math libraries implementing large integer division and multiplication will actually use shifting when possible to save time.
5
Operators – Assignment Operators □=
Oftentimes, programmers will want to do as much as they can in a single line of code. Without constructs like conditional expression and pre-/post- in/decrement, there's an eventual stopping point where you need to move onto the next line. int exp = var += value; Assignment operators are a form of syntactic sugar which enables you to perform variable assignment as part of an expression.
6
Operators – Assignment Operators □=
Oftentimes, programmers will want to do as much as they can in a single line of code. Without constructs like conditional expression and pre-/post- in/decrement, there's an eventual stopping point where you need to move onto the next line. int exp = var □= value; Assignment operators are a form of syntactic sugar which enables you to perform variable assignment as part of an expression. The box is a placeholder we'll talk about what goes in there shortly.
7
Operators – Assignment Operator =
The = operator we should already be familiar with from normal assignment statements. However, it can also be used as an expression, where it takes the value of both sides. int exp = var = value; The reason that this isn't ambiguous is that the assignment statement itself says "take the value on the right and store it in the location on the left." Since both var and value will be the same value after assignment, we know what is going to be stored in the variable exp.
8
Operators – Assignment Operators □=
However, assignment as an expression doesn't stop with the usual assignment. int exp = var □= value; The C language actually includes a wide variety of assignment operations which allow us to save space. For every binary operation in C, there's an equivalent assignment operator for it. int exp = var += value; /* same */ int exp = var = var + value; /* same */
9
Operators – Assignment Operators □=
Again, it's not ambiguous what the value of the entire expression is going to be because of the nature of assignment. int exp = var □= value; The can also be used by themselves as a shortcut. var □= value; Here's a list of assignment operators. Arithmetic a += b, a -= b, a *= b, a /= b, a %= b Bitwise a >>= b, a <<= b, a &= b, a |= b, a ^= b
10
Fun Tidbit – Revisiting XOR Swap Example
Dummy Variable Swap int dummy = a; a = b; b = dummy; XOR Swap from Tuesday a = a ^ b; b = a ^ b; XOR Swap using Assignment Operators a ^= b ^= a ^= b; Later, try it out on paper for yourself! Remember, assignment operators are evaluated from right to left.
11
Operators – Modulo % Remember that when working in programming languages, division between integers with the / operator is assumed to be integer division. Remainders are dropped and the result is also an integer. Many times, the remainder is more valuable to us than the division. int rem = a % b; The modulo (%) operator between two numbers evaluates to the remainder of the first operand divided by the second. Similar to division, undefined when the second operand is 0. In general, the modulo operator tries to maintain the following relationship. (a / b) * b + a % b == a
12
Modulo – Table Indexing
/3 %3 1 2 3 4 5 The modulo operator is very flexible since it describes a number of relationships both numerically and spatially. On the right, you'll see that we can use the % and / operators together to assign a unique index to a 2-dimensional table of width 3. In this way, we have a left-to-right, top- to-bottom zigzag over all the cells of the table. x = n % 3; y = n / 3; n = x + y * 3; Y\X 1 2 3 4 5
13
Modulo – Digit Extraction
Another neat trick with modulo is that we can use it to extract portions of a number. Remember, that numbers can be expanded in any representation as a weighted sum of powers. Example: 2018 = 2(1000) + 1(10) + 8(1) Note that regardless of which base, all but the rightmost term share a common factor (the base itself). Decimal (2018) 2018 = 2(1000) + 1(10) + 8(1) Hexadecimal (0x7E2) 2018 = 7(256) + E(16) + 2(1) Octal (03742) 2018 = 3(512) + 7(64) + 4(8) + 2(1) 2018 % 10 = 8 2018 % 16 = 2 2018 % 8 = 2
14
Modulo – Digit Extraction
Using this property of modulo, we can develop an algorithm which retrieves one digit at a time from a numeric value. Moreover, this allows us to do this in any base! For bases less than 10, this should be enough. (Why not ≥10?) char lastdigit = (value % base) + '0'; Now, this only gives us the right-most digit of the number in that base representation. If we want to get each digit, we're going to have to find some way to shift the digits of the number to the right for non-powers of two.
15
Digit Extraction This isn't actually that hard, but it requires a good understanding of base representation. To review, every number has a representation in every base as a weighted sum of powers. 2018 = 2(1000) + 0(100) + 1(10) + 8(1) If we've already extracted the last digit (8) then we want to turn the number we have into the one with that digit removed. This can simply be done with integer division. 2018/10 = 201 = 2(100) + 0(10) + 1(1)
16
Digit Extraction – Final Remarks
So the takeaways for extracting the digits of a numeric value in another base are as follows: Use modulo to extract the right-most digit in that base. Use integer division to shift the number to the right by one digit in that base. If you repeat this process until you reach 0, you'll have extracted every digit in the number. There is no itoa function included in the standard library stdlib.h You can use fprintf to output in decimal, hexadecimal, and other bases (check the format table in the documentation. For in-memory options, sprintf is an option that I hope to discuss soon.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.