C Puzzles Taken from old exams

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

Computer Science School of Computing Clemson University Introduction to Mathematical Reasoning Jason Hallstrom and Murali Sitaraman Clemson University.
Intro to Computer Systems Recitation #1 By sseshadr.
When NOT to use Unsigned? Don’t Use Just Because Number Nonzero – C compilers on some machines generate less efficient code unsigned i; for (i = 1; i
Fabián E. Bustamante, Spring 2007 Integers Today Numeric Encodings Programming Implications Basic operations Programming Implications Next time Floats.
CS213 Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules CS 213 F’00.
“The course that gives CMU its Zip!” Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations.
CS213 Integers Apr 3, 2006 Topics Numeric Encodings
True/False. False True Subject May Go Here True / False ? Type correct answer here. Type incorrect answer here.
“The course that gives CMU its Zip!” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
February 4, 2003 CSCE 212 Computer Architecture Lecture 3 Representing Integers.
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Find opposites of numbers EXAMPLE 4 a. If a = – 2.5, then – a = – ( – 2.5 ) = 2.5. b. If a =, then – a = –
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
1 Integer Representations. 2 Outline Encodings –Unsigned and two’s complement Conversions –Signed vs. unsigned –Long vs. short Suggested reading –Chap.
Chapter 2 Bits, Data Types & Operations Integer Representation
Comp Integers Spring 2015 Topics Numeric Encodings
– 1 – CS 105 Computer Systems Introduction Topics: Staff, text, and policies Lecture topics and assignments Lab rationale CS 105 “Tour of the Black Holes.
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Practice 1.2 Answers
Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1.2 Patterns and Inductive Reasoning. Ex. 1: Describing a Visual Pattern Sketch the next figure in the pattern
University of Washington Integers The Hardware/Software Interface CSE351 Winter 2013.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
 When a conditional and its converse are both true, we combine them to make a biconditional.  Example:  Biconditional – An angle is right if and only.
CS 105 “Tour of the Black Holes of Computing” Topics Numeric Encodings Unsigned & Two’s complement Programming Implications C promotion rules Basic operations.
Int’s and Integers CENG331: Introduction to Computer Systems 3 rd Lecture Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the.
“The course that gives CMU its Zip!” Topics Basic operations –Addition, negation, multiplication Programming Implications –Consequences of overflow.
Integers Topics Representations of Integers Basic properties and operations Implications for C.
Direct Proof and Counterexample I Lecture 11 Section 3.1 Fri, Jan 28, 2005.
CS2100 Number Systems Supplementary Notes 1 NUMBER SYSTEMS SUPPLEMENTARY NOTES  Complements  Floating-point Numbers.
Topics Numeric Encodings –Unsigned & Two’s complement Programming Implications –C promotion rules Basic operations –Addition, negation, multiplication.
Patterns and Inductive Reasoning. Inductive reasoning is reasoning that is based on patterns you observe. If you observe a pattern in a sequence, you.
– 1 – CS 105 Computer Systems Introduction Topics: Class Intro Data Representation CS 105 “Tour of the Black Holes of Computing!” Geoff Kuenning Fall 2014.
Lesson 1-7 Inductive Reasoning. Inductive Reasoning – making conclusions based on patterns you observe. Conjecture – conclusion you reach by inductive.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Today’s Instructor: Randy Bryant
Exercise Simplify – 22. – 4.
Integers Topics Numeric Encodings (2.2) Programming Implications
CS 367 Integers Topics (Ch. 2.2, 2.3) Numeric Encodings
Topics IEEE Floating Point Standard Rounding Floating Point Operations
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
University of Washington
Integer Representations and Arithmetic
“The course that gives CMU its Zip!”
CS213 Integers Topics Numeric Encodings Programming Implications
CS 105 “Tour of the Black Holes of Computing”
CS 105 “Tour of the Black Holes of Computing”
Comp Integers Spring 2015 Topics Numeric Encodings
Computer Systems Introduction
Bits, Bytes, and Integers January 16, 2008
Set includes rational numbers and irrational numbers.
Bits, Bytes, and Integers 2nd Lectures
Integers Topics Numeric Encodings Programming Implications
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Representing Information (2)
Arithmetic Topics Basic operations Programming Implications
Computer Organization COMP 210
Integer Representations Jan. 23, 2001
Computer Organization COMP 210
Computer Organization COMP 210
Topics discussed in this section:
Computer Systems Introduction
Lecture 2: Bits, Bytes, Ints
Presentation transcript:

C Puzzles Taken from old exams Assume machine with 32 bit word size, two’s complement integers For each of the following C expressions, either: Argue that is true for all argument values Give example where not true x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x <= 0  -x >= 0 Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y;

C Puzzle Answers Assume machine with 32 bit word size, two’s comp. integers TMin makes a good counterexample in many cases x < 0  ((x*2) < 0) ux >= 0 x & 7 == 7  (x<<30) < 0 ux > -1 x > y  -x < -y x * x >= 0 x > 0 && y > 0  x + y > 0 x >= 0  -x <= 0 x <= 0  -x >= 0 x < 0  ((x*2) < 0) False: TMin ux >= 0 True: 0 = UMin x & 7 == 7  (x<<30) < 0 True: x1 = 1 ux > -1 False: 0 x > y  -x < -y False: -1, TMin x * x >= 0 False: 30426 x > 0 && y > 0  x + y > 0 False: TMax, TMax x >= 0  -x <= 0 True: –TMax < 0 x <= 0  -x >= 0 False: TMin

Puzzle number 6, x. x >= 0, is indeed false Puzzle number 6, x * x >= 0, is indeed false. But the counter-example of 30426 given on the slide is incorrect. A real counter-example is 2^16+2^14. You can write a simple C program to verify my assertions.