1 / 8

CS Club Meeting 4 10/10/13

CS Club Meeting 4 10/10/13. Meeting Topic: Integers. int s in Java and C++ are 4 bytes and signed long long (C++) and long (Java) are 8 bytes. There are 2^32 different values an int can take on. The smallest is -2^31, the largest is 2^31-1

farica
Download Presentation

CS Club Meeting 4 10/10/13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS Club Meeting 4 10/10/13

  2. Meeting Topic: Integers • ints in Java and C++ are 4 bytes and signed • long long (C++) and long (Java) are 8 bytes. • There are 2^32 different values an int can take on. • The smallest is -2^31, the largest is 2^31-1 • ints are stored as binary numbers. Each of the 32 bits (binary digit) is either 0 or 1 • The first bit is a sign bit; if it is a 1, the number is negative, otherwise it is zero or positive. • The int 1 is represented as 31 0’s followed by a 1 • And the int 0 is 32 0’s.

  3. Two’s Complement Representation • Binary numbers are implemented using “2’s complement” • -1 is 11111111… • Why? • Let’s try decrementing 8 (0000…1000) • 8 - 1 = 7 (0000…0111) • Decrementing 0 (0000…0000) gives you… • 0 - 1 = -1 (1111…1111) • In general, -x + (x-1) = -1 • -x and x-1 are complements; -x = ~(x-1) • ~ is the NOT operator. It flips every bit of an integer • NOT 01111001 is 10000110

  4. Two’s Complement Representation • This makes addition easier and makes sense too. • Let’s try 10’s complement in decimal. • 4 digit decimal integer: ~x is 9999 - x • ~ 1234 = 8765 • Let’s try subtraction • - 3587 = ~3587 + 1 = 6412 + 1 = 6413 • 7892 - 3587 = 7892 + 6413 = 14305 • Keep only last 4 digits: 7892 - 3587 = 4305! • Allows you to do subtraction with addition. • Note that ~x + 1 = 9999 - x + 1 = 10000 - x

  5. Integer Overflow • Integer overflow is when arithmetic attempts to create a value that is too large • Arithmetic with large integers often leads to overflow • If you do 100000 * 100000, you won’t get 10000000000 because that doesn’t fit in an int • In fact, you’ll get 1410065408 (10^10 mod 2^32) • Be careful, even if you use a long! int x = 100000; long long y = x * x; This will still overflow!!!

  6. Integer Overflow Continued • What is 011111… + 1? • This is the transition from INT_MAX to INT_MIN • For what value of x is abs(x) < 0? • SPOILER ALERT! The answer is coming up. • x = INT_MIN (-2^31). int abs(int x) { if (x > 0) return x; else return -x; }

  7. Addition is Easy • Given two base 10 integers a and b, find their sum. • Input Format: • Line 1: Two numbers, a and b. • Output Format: • Line 1: One number, a + b. • Constraints • 5 points: -10^9 <= a,b <= 10^9 • 10 points: -10^18 <= a,b <= 10^18 • 20 points: 0 <= a,b <= 10^100 • 25 points: -10^100 <= a,b <= 10^100

  8. Sample Case Input: -10 999 Output: 989 Sample Explanation: -10 + 999 = 989.

More Related