Lecture 1 - Binary and Operator Overloading

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Slide 40: What do you have to do if you use uint_max, int_max?

#include<climits> for integer types #include<cfloat> for floating point types

Slide 52: What is the algorithm to convert from decimal to binary?

1. n % 2 (gets bits going from right to left, smallest to largest) 2. n/2 3. repeat until n = 0 edge cases: initial input is 0 - push back 0 and ur done

Slide 95: How do you overload comparison operators?

Define < and =, then the rest just call the first two. Usually, the first two are members, rest are nonmembers.

Slide 9: What happens in machine code when you write "int a = 5"?

The computer reserves a chunk of memory (usually with 32 bits), converts 5 into binary.

Slide 85: What is the difference between + and +=?

+: symmetric, operands are not modified, new object is returned +=: not symmetric, a is modified but not b, does not need to create a new object, but returns a by reference for chaining

Slide 117: How does computer interpret all 0s with 1 in signed bit? Why?

-128. This is a boundary case. Why? Cos they already had a definition for 0, so they decided to use -0 to represent something else.

Slide 99: How would you compare binary numbers?

0. make sure there are no leading zeroes (private clean_up function) 1. see which one has more bits. 2. if same, compare bitwise.

Slide 121: What can't you bitshift?

1. Can't bitshift BY a negative number. 2. Don't bitshift outside integer types, will produce undefined behavior. 3. can't bitshift double types.

Slide 113: How do you store negative numbers?

1. Sign and magnitude 2. One's complement 3. Two's complement

Slide 81: What happens when you do this? int c = 10 int f = ++(++c); int g = (c++)++;

1. c = 10 2. f = 12, c = 12 3. compiler error! fck postfix ++

Slide 139: What happens if there is ambiguity as to how a conversion should be made?

Compiler error

Slide 142: What happens if you define an operator that converts to unsigned ints, and then have this code: Integer a = 17; a+2;

Compiler error! Ambiguity caused by two options: either convert the 2 to an Integer, or convert the a to an unsigned int

Slide 135: What happens when you do this? Integer a = 17; a+2;

Compiles! Because compiler uses Integer constructor to convert 2 into an Integer. This automatic conversion is called COERCION.

Slide 140: What do these operator do? operator unsigned int(); operator double();

Conversion operator! Allows you to cast your object to another data type.

Slide 108: What does bit-shifting do?

Either multiply by 2, or divide by 2 and take the floor.

Slide 68: What do you have to do to your operator to make chaining possible?

Have it return by reference

Slide 125: What else can you use theseoperator for? Integer Integer::operator+() const; Integer Integer::operator-() const;

Making numbers positive or negative

Slide 33: What happens if you go past the largest value on an unsigned int? Why? e.g. unsigned int a = uint_max + 1;

Prints out 0. Why? The result is reduced modulo the number 1 greater than the largest value that can be represented. In other words, a = 4294967295 % 4294967296 which is 0.

Slide 116: What is two's complement?

Reverse all the bits, and then add 1. If you add 19 and two's complement of 19, you get all 0s, and a 1 that goes outside the scope of the bits. e.g. 19: 0 0 0 1 0 0 1 1 ~19: 1 1 1 0 1 1 0 0 (one's complement) ~19+1: 1 1 1 0 1 1 0 1 (two's complement)

Slide 115: What is one's complement?

Reverse all the bits. Makes sense, but if you add binary 19 and one's complement of 19 together, you get all 1s, which is not equal to 0.

Slide 14: How do you do binary addition?

Same as & operation 1&1 = 0 (carry the 1) 1&0 = 1 0&0 = 0

Slide 120: What happens when you bit shift negative numbers?

ShiftLeft: append 0 to the right side. ShiftRight: append 1 to left side.

Slide 10: What is a byte?

The smallest amount of memory that can be allocated at one time. NOT necessarily 8 bits. Usually is. 8 bits = octet.

Slide 49: How does operator= (the assignment operator) work for custom classes?

There is a default implementation of operator= defined for all classes. It copies over each element of the class to the other object. This is why you can copy construct custom objects.

Slide 109: How are operator >>= and <<= declared?

They return an integertype&, so that you can chain them but like why would you

Slide 123: What kind of operator is this? Integer Integer::operator+() const;

This is the unary integral promotion operator. Integral promotion is when an int type variable is convert to a long, or when a short is converted to an int. Why do integral promotion? When arithmetic will overflow

Slide 123: What kind of operator is this? Integer Integer::operator-() const;

This operator participates in integral conversion. Converts variables from unsigned to signed. May lose information, get undefined quantities, if unsigned value is outside signed range.

Slide 37: What happens if you go past the largest value on a regular int? e.g. int a = int_max + 1;

Undefined behavior.

Slide 143: How do you avoid coercion?

keyword explicit: put in front of constructors, prevents compiler from automatically creating objects using that constructor e.g. public: explicit Integer(unsigned int);

Slide 82: What is the difference between prefix and postfix ++?

prefix: returns by reference, makes chaining possible modifies the object itself postfix: makes a copy of the object, modifies the original, then returns the copy of the object - takes an unused int parameter Why? that's how the compiler tells them apart

Slide 19: What does this code do? int a = 19; std::cout << ~a << std::endl;

prints -20. The negation of 19 is -20.

Slide 11: What's the different between signed and unsigned ints?

signed - reserves last bit for sign, other 15 bits represent integer

Slide 29: What is the largest number you can store in an int? Unsigned int?

signed: 2^31-1 = roughly 2 million unsigned: 2^32-1 = roughly 4 billion how do you get this number in code? uint_max

Slide 39: What does sizeof() do?

takes input of any variable, returns size_t variable containing number of BYTES variable has reserved for itself.

Thought: a mutator does not need to return the value it modifies.

thinking emoji

Slide 80: What is *this?

this is a pointer to the object invoking the member function *this is a reference to the object invoking the member function

Slide 23: What is the largest number you can store in an unsigned short?

unsigned short - 15 bits: 2^15-1 = 32767

Slide 44: How do you store a number bigger than the maximum size of any fundamental data type?

vector<bool>


Kaugnay na mga set ng pag-aaral

CompTIA A+ 220-1101 Exam Acronyms Quiz

View Set

Walt Whitman and Emily Dickinson Test

View Set

Literary Devices, elements, and techniques

View Set

Project Management - Ch. 10 MCQ only

View Set

Ch2: Life Insurance: Life Insurance - Life Basics

View Set