Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.

Similar presentations


Presentation on theme: "CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression."— Presentation transcript:

1 CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression

2 Operators – Bitwise Operations
Bitwise operations are operations that are performed at the binary bit level on a value. The logic for these operations comes from the function of their corresponding logic gate in electronics and circuitry. You'll be working with these more in CS341. In programming, these elementary operations are often helpful for solving problems at the higher level, so they're included into the language.

3 Operators – Bitwise AND &
The Bitwise AND (&) operator takes two numeric operands and gives a single numeric in response. char flags = flags & 0x0F; For each column of bits across both numbers, the resultant value will have a 1 bit only if both bits from the operands are 1. Otherwise, the bit for that column in the result is 0. It doesn't matter which order the operands are in (commutativity). However, generally, it's said that the second operand is "turning off flags" in the first operand. & =

4 Operators – Bitwise OR |
The Bitwise OR (|) operator takes two numeric operands and gives a single numeric in response. char flags = flags | 0x0F; For each column of bits across both numbers, the resultant value will have a 1 bit only if at least one bit from the operands are 1. Otherwise, the bit for that column in the result is 0. The Bitwise OR is also commutative. It's said that the second operand is "turning on flags" in the first operand. | =

5 Operators – Bitwise XOR ^
The Bitwise XOR or Exclusive OR (^) operator takes two numeric operands and gives a single numeric in response. This is NOT the Exponent Operator and NOT how you take bases to powers! char flags = flags ^ 0x0F; For each column of bits across both numbers, the resultant value will have a 1 bit only if exactly one bit from the operands are 1. You can also think of it as the 1 bits from the second operand "flipping" the bits of the first operand. (Note: 0 does not flip anything) This operation is also commutative. ^ =

6 Operators – Bitwise NOT ~ / One's Comp.
We've actually already covered Bitwise NOT (~) before as a concept, rather than an operator. It's the One's Complement! char flags = ~flags; It's a unary operator which operates on numeric values. For each bit in the operand, the resultant value has that bit flipped. This is mathematically equivalent to (–x – 1) ~ =

7 Bitwise – Truth Tables Truth Tables can be used to describe a bitwise operation column-by-column. Go column-by-column through both operands' binary representations and use the table to determine the bit value for that column in the result. AND 1 OR 1 1 1 1 1 1 1 Operands XOR 1 NOT Results 1 1 1 1 1

8 Bitwise – Hexwise Any bitwise operation can be done equivalently on other power-of-two representations of the numbers. We can do this for hexadecimal by grouping the binary digits into fours and replacing them with their hexadecimal digits. Note, since the bitwise operations are always the same for the groups of four bits, they'll be the same for the hex digits. 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 1010 B 1011 C 1100 D 1101 E 1110 F 1111 7F & A0 & = 20 =

9 Bitwise – Table for Hexadecimal
AND F E D C OR F E D These tables would get really huge, just get familiar with doing these operations in groups of 4 bits at a time. As an exercise on your own time, try making the full tables for AND and OR. Operands Results

10 Examples - Bitwise Operations & and |
0x & & & & 0x = = = = 0x 0x | | | | 0x = = = = 0x

11 Examples - Bitwise Operations
The XOR operator requires a little more care in discussion. Note that the last example of it can turn one message into another. 0xCAFEBABE ^ ^ ^ ^ 0x = = = = 0xBEEFCACE

12 Encryption with XOR One very powerful property of XOR that the other bitwise operations don't have is the recoverability of past operands. newx = x ^ key; oldx = newx ^ key; This is possible because taking the XOR with respect to key gives us x with all of the key bits flipped. If we do this twice, every bit in x is either never flipped or flipped twice (which brings it back to it's starting point). x ^ n ^ n = x ^ 0 = x for all x and n

13 Type Conversion – String to Numeric atoi
We learned last week how to work with fscanf to read numeric values from input. However, once values are already in memory as strings, fscanf won't help us work with them as numeric values. int num = atoi(str); In stdlib.h, there is a function called atoi which converts a number in a string to a numeric int value. The function ignores any whitespace characters before the representation in the buffer given and it can be followed by any non-numeric characters. Will return 0 on failure to find a numeric string at the beginning of the buffer.

14 Type Conversion – How to implement atoi
Remember from previous lectures that every digit in decimal representation is the coefficient of a power of 10. To implement atoi, we simply need to take each ASCII digit to it's numeric counterpart ('0'  0) Then multiply it by the power of 10 corresponding to that column. Finally, total everything up and that's the numeric value of that string. int atoi(char s[ ]) { int i, n = 0; for (i=0; s[i] >= '0' && s[i] <= '9'; ++i) n = 10 * n + (s[i] - '0'); return n; }

15 Fundamentals – Conditional Expression ?:
This is one of the only ternary operations you’re likely to find in most programming languages. An operation that takes 3 operands. int max = (a > b) ? a : b; The conditional (?:) operator is an in-place if-expression which has three parts: the condition, the then-value, and the else-value. If the condition is non-zero (true), the entire expression evaluates to the then-value. Otherwise, the entire expression evaluates to the else-value.


Download ppt "CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression."

Similar presentations


Ads by Google