CC Operators Binary Operators Operators Between Two Operands
C/C++ Operators Binary Operators: Operators Between Two Operands: Operator Meaning + * / % Addition Subtraction Multiplication Division Modulus Example Definition. x = 6 + 2; x = 6 - 2; x = 6 * 2; x = 6/2; x = 6 % 2; Add the values on either side of + Subtract right value from left value Subtract right hand side values Divide left value by right value Remainder of left divided by right What about exponentiation? ? ? Either x = 6 ^ 2 OR x = 6 ** 2 ? ? ? No Such Thing: You must write your own function (Or use an available one) NOTE: Dividing 2 real numbers yields EXACT results Dividing 2 Integers yields TRUNCATED results Why? ?
C/C++ Operators • Previously, we talked about how integers were stored. • For example, we said the declaration short s 1 = 56, s 2 = 17; Asks for two 16 -bit integer locations (s 1 and s 2) and stores the values 56 and 17 in them (respectively) Var/Loc s 1: 0 0 0 0 0 1 1 1 0 0 0 5610 Var/Loc s 2: 0 0 0 1 0 0 0 1 1710 We know that if we divide 56/17 (on a calculator) you would get the value 3. 2941176470588235294117647058824 But, how do you store a mantissa as an integer? ? (You don’t) OK – But what if I wanted to get the real answer above? ? All Integers can be rewritten (as floats (casting)) The opposite is NOT true Decimal to Binary Converter
C/C++ Operators Consider (i. e. , enter, build and run) the code below: #include "stdafx. h" #include <iostream> using namespace std; void main() { int s 1 = 56, s 2 = 17; cout << s 1 << " divided by " << s 2 << " is " << s 1/s 2 << endl; cout << "Casting " << s 1 << " and " << s 2 << " and then dividing yields: " << (float)s 1/(float)s 2 << endl; } The output should appear as:
C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator Add the following line to your program, compile and run: cout << "The remainder of 9/2 is " << 9 % 2 << endl; How is this useful? ? There a number of uses. We are going to take a look at one example: Converting decimal values to binary
C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator Add the following line to your program, compile and run: cout << "The remainder of 9/2 is " << 9 % 2 << endl; How is this useful? ? There a number of uses. We are going to take a look at one example: Converting decimal values to binary
Converting from decimal to binary Dividing any integer by 2 means that the remainder must be either 0 or 1 A few slides ago, we noted that 5610 = 1110002 How can we verify this? ? When you first learned division, you probably were taught to do it as follows: 28 2 56 -56 0 14 2 28 -28 0 7 2 14 -14 0 2 goes into 56, 28 times. 2 times 28 is 56. Subtracting -56 from 56 leaves a remainder of 0 2 goes into 28, 14 times. 2 times 14 is 28. Subtracting -28 from 28 leaves a remainder of 0 2 goes into 14, 7 times. 2 times 7 is 14. Subtracting -14 from 14 leaves a remainder of 0 3 2 7 -6 1 1 2 3 -2 1 0 2 1 -0 1 2 goes into 7, 3 times. 2 times 3 is 6. Subtracting -6 from 7 leaves a remainder of 1 2 goes into 3, 1 times 3 is 3. Subtracting -3 from 7 leaves a remainder of 1 2 goes into 1, 0 times 1 is 0. Subtracting 0 from 1 leaves a remainder of 1
Converting from decimal to binary Let’s rewrite this procedure in a simpler form Gathering the remainders from last to first: Pass 0 remainder 2 56 0 quotient 1 2 28 0 2 2 14 0 3 2 7 1 4 2 3 1 5 2 1 1 0 Stop when the quotient becomes 0 1 1 1 0 0 0 = 56 How do we do that? ? We’re going to have to go over your first abstract data type: An Array
Arrays An array is an arrangement of similar basic data types Let’s create an array of short integers short binary[15], temp[15], decval = 56, reverse = 0, index = 0, i; Next, we’ll create the array as we did before while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; Now we need to reverse the order of the array i = index; while (reverse <= index) { binary[reverse] = temp[i]; reverse++; i--; } The complete programming (including the printing of the result) is given on the next slide
Arrays #include "stdafx. h" void main() { short binary[15], temp[15], decval = 177, reverse = 0, index = 0, i; while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; for (i = 0; i <= index; i++) printf("%d", temp[i]); printf("nn"); i = index; while (reverse <= index) { binary[reverse] = temp[i]; printf("%d %d %dn", i, reverse, binary[reverse]); reverse++; } i--; for (i = 0; i <= index; i++) printf("%d", binary[i]); printf("n"); } As usual, you should enter the code, compile it, and run it
Assignment Operators: Operators Assigning Values to RAM: Operator = += = Example x = 6 + 2; x += 3 x =3 *= x *= 3 /= x /= 3 %= x %= 3 Definition. Location x will get the value 6 + 2 ( = 8) Same as x = x + 3: IF x contained the value 4, it now contains the value 7 Same as x = x - 3: IF x contained the value 4, it now contains the value 1 Same as x = x * 3: IF x contained the value 4, it now contains the value 12 Same as x = x / 3: IF x contained the value 4: IF x is a float (real), it now contains 1. 33 IF x is an integer, it now contains the value 1 Same as x = x % 3: IF x contained the value 4, it now contains the value 1 (3 goes into 4 Once with ONE left over)
Unary Operators: Operators on a single Operand: Operator + ++ -- Meaning Minus Sign Plus Sign Increment OR Decrement OR Example x = -2; x = ++y; x = y++; x = --y; x = y--; Definition. Assign a negative value Assign a Positive value Prefix Notation Postfix Notation Prefix and Postfix Notation? ? What’s that all About ? ? ? Consider the following section of c code: int x, y, a, b; a = b = 1; x = 2 * ++a; // Initialize the variables (locations) with the value 1 // PREFIX Notation: First increment a by 1 (a = 2) // Then Multiply by 2 & assign to x (x=2*2) y = 2 * b++; // POSTFIX Notation: First multiply b by 2 & assign to y (y=1*2) // Then increment b by 1 (b = 2) printf("%d %dn", x, y); // The Output would appear as: 4 2
There are two (2) Additional Unary Operators: sizeof Return the size of the operand (in bytes) Consider the following section of c code: int i = 12; float f = -123. 45; printf("%d sizeof = %dn", i, sizeof i); printf("%7. 2 f sizeof = %dn", f, sizeof f); (type) // would produce: 12 sizeof = 2 // would produce: -123. 45 sizeof = 4 Convert a value to the specified data type Consider the following section of c code: int a = 4; float b = 12. 245; printf("%d %ldn", a, (long) a); printf("%d %fn", a, (float) a); printf("%f %dn", b, (int) b); } // would produce: 4 4. 000000 12. 245000 12
Relational Operators: Operators on two Operands Yielding a T/F Answer: Operator Meaning Example < x < 2; Less Than x <= 2; Less Than <= OR Equal To == x = = 2; Equal To != Not Equal To x ! = 2; > Greater than x > 2; >= Greater than x >= 2; OR Equal To x > 2 && And && x<4 x==2 || || Or x==4 ! Not ! (x = = 2) Definition. False if x contains the value 2 True if x contains the value 2 False if x contains the value 2
Order of Operations: Order 1 2 3 4 5 6 7 8 9 Operator ( ) + - ++ -- (unary) ! (Rel) * / % + - (binary) < > <= >= == != && || = Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; What is the value of the statement: a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1;
Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; - 96 + 1 = - 95 0 - 96 = - 96 384 / 4 = 96 1 / 5 = 0 48 * 8 = 384 1 * 1=1 5 % 4=1 2+3=5 42 + 6 = 48 6 * 7 = 42 a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1;
Example 2: Given: Evaluate: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; ++a * b-- + c % d / e - f-- * g + 6 * h++ / b + h; 2 2*2=4 3%4=3 3/5=0 6 * 7 = 42 6 * 8 = 48 48 / 2 = 24 4+0 = 4 4 - 42 = - 38 + 24 = - 14 + 8 = -6 Note that now: a = 2, b = 1, f = 5, h = 9
Example 3: Given: int a = 1, b = 2, c = 3, d = 4; Consider the statement: a > b || b < c && c == 3 || d < 4 && b != c False True True
Conversion specifiers for scanf and printf: printf Specifier(s) %c %d %i %u %ld %lu %f %e %E %g %G %Lf %Le %o %x %X %s %p %% Output Single Character Signed decimal Integer Unsigned decimal Integer Signed/Unsigned long Integer Signed floating-point, decimal notation Signed floating-point, e or E notation Use shorter or %f or %e (%f or %E) Long double floating-pt (also %LE) Unsigned octal notation Unsigned hexadecimal notation Character string Pointer (address) Print a % sign Example B 457 7832 -345 64 -6. 576 -4. 5 e 3 2. 1 E-2 -2. 1 4. 56 E 4 7. 32 -6. 1 e 4 4271 4 d 2 a F 6 B Hello 4 FF 0: 8 BC 1 % Additional information about printf/scanf specifiers can be found in the Supplementary Materials Link
Additional Precompiler Directives User Defined constants: #define PI = 3. 15149 User Defined constants: #define SQUARE (x) x * x // macro example. cpp : Defines the entry point for the console application. #include "stdafxh" #include <iostream> using namespace std; #define PI 3. 15149 #define SQUARE(X) X*X void main() { float area, r = SQUARE(3. 5); area = PI * r; cout << "The area is: " << area << endl; }
- Slides: 23