Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Similar documents
Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Floating Point January 24, 2008

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Foundations of Computer Systems

Giving credit where credit is due

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Giving credit where credit is due

System Programming CISC 360. Floating Point September 16, 2008

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

COMP2611: Computer Organization. Data Representation

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

Computer Organization: A Programmer's Perspective

Systems Programming and Computer Architecture ( )

Floating Point Numbers

Floating Point Numbers

Today: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j

CS429: Computer Organization and Architecture

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

CS356: Discussion #3 Floating-Point Operations. Marco Paolieri

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Data Representation Floating Point

Data Representation Floating Point

Data Representation Floating Point

Integers and Floating Point

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Description Hex M E V smallest value > largest denormalized negative infinity number with hex representation 3BB0 ---

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Representing and Manipulating Floating Points. Jo, Heeseung

The course that gives CMU its Zip! Floating Point Arithmetic Feb 17, 2000

Representing and Manipulating Floating Points

Computer System and programming in C

CS 3843 Final Exam Fall 2012

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Integer Representation Floating point Representation Other data types

Computer Systems Programming. Practice Midterm. Name:

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Real Numbers. range -- need to deal with larger range, not just [not just -2^(n-1) to +2^(n-1)-1] - large numbers - small numbers (fractions)

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

15-213, Fall 2007 Midterm Exam

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Representing and Manipulating Floating Points

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Inf2C - Computer Systems Lecture 2 Data Representation

15213 Recitation 2: Floating Point

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Name: Login: cs61c- Decimal Ternary 5 12 three three

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Representing and Manipulating Floating Points

Floating Point Numbers

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

CMPSCI 145 MIDTERM #2 SPRING 2017 April 7, 2017 Professor William T. Verts NAME PROBLEM SCORE POINTS GRAND TOTAL 100

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Number Representation & Conversion

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Finite arithmetic and error analysis

Department of Electrical Engineering and Computer Sciences Spring 2007 Instructor: Dr. Dan Garcia

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Chapter 4: Data Representations

World Inside a Computer is Binary

Introduction to Computer Science. Homework 1

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Floating Point Arithmetic

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

Written Homework 3. Floating-Point Example (1/2)

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Floating Point Arithmetic

As stated earlier, the declaration

CS367 Test 1 Review Guide

Numeric Encodings Prof. James L. Frankel Harvard University

Computer Systems Principles. C Pointers

CS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

SIGNED AND UNSIGNED SYSTEMS

EE 109 Unit 20. IEEE 754 Floating Point Representation Floating Point Arithmetic

Internal representation. Bitwise operators

Floating Point Arithmetic

CMPSCI 145 MIDTERM #2 Solution Key SPRING 2018 April 13, 2018 Professor William T. Verts

CS , Fall 2007 Exam 1

Computer Architecture I Midterm I

Transcription:

Section 1: Multiple choice (select any that apply) - 20 points 01. Representing 10 using the 4 byte unsigned integer encoding and using 4 byte two s complements encoding yields the same bit pattern. (a) True (b) False 02. Using rounding to the nearest even and rounding to nearest 1/4 (2 bits right of binary point), 10.11100 becomes.. (a) 10.10 (b) 11.00 (c) 10.01 (d) 10.00 (e) None of the above 03. Assuming the following is in the context of a well-formed program, what is the result of attempting to compile and execute this code? int* p = NULL; printf("%d", *p); (a) Will not compile (b) Segmentation fault (c) 0 will be printed (d) Garbage will be printed (e) None of the above 04. In the absence of explicit initialization the following is true about variables in C. (a) global variables will contain garbage (b) static variables will contain garbage (c) local variables will contain garbage (d) All of the above 05. Assuming the following is in the context of a well-formed program which of the logical statements are true? float a = -0.0; int b = *((int*)&a); int c = INT_MAX + 1; (a) b!= c (b) b > c (c) b < c (d) b == c (e) None of the above 06. Suppose 'int x = 0x00FF;' and 'int y = 0xFF00', which of the following evaluates to false if used in an if-statement? (a) (x & y) (b) (x y) (c) Both (d) Neither

07. Things that must be allocated in heap memory include... (a) variables declared in a function that must survive after the function call completes (b) variables declared in a function that must retain a value between invocations (c) variables that are to be globally accessible (d) None of the above 08. In C, if x is an integer variable, the expression x << 3 computes x * 8 but does not change the value of x. (a) True (b) False 09. Which of the following types has a largest size (in terms of bytes), assuming 64-bit machine? (a) int* (b) float* (c) double (d) long (e) All are the same size 10. The following bit vector: 011101 can be interpreted as... (a) An unsigned binary number (b) A two s complement binary number (c) A 6-bit IEEE 754-like encoded floating point number (d) All of the above Grading Guide: Every question is worth 2 points. Each one only has one correct answer. If they select the correct answer, but additional incorrect answers, reduce by one point for each incorrect answer. In other words, if the student picks two answers, one of them being the correct one, then they get one point. If they select the correct answer and two additional incorrect answers, then they get zero points.

Section 2: Bit Twiddling - 15 points 01. Initialize 'mask' such that when it is AND'ed with 'a' the results is the last three bits of 'a' (e.g. the 1's 2's and 4's bit). short a = 0b101010101; short mask = 0b000000111; assert((a & mask) == 0b000000101); 02. Assume char x = 0x3B and char y = 0x77 What is the value of z in decimal after the following line of code? char z = (x ^ y) y; 127 03. Assume char x = -1 What is the value of z in decimal after the following line of code? char z = x >> 7; -1 Grading Guide: These are all or nothing. 5 points per question.

Section 3: C Code Reading - 15 points 01. void foo(int x, int y) { int t = x; x = y; y = t; int main() { The following will be printed... int x = 1; int y = 2; printf("x=%i, y=%i\n", x, y); x=1, y=2 foo(x, y); printf("x=%i, y=%i\n", x, y); x=1, y=2 return 0; 02. int main() { short a[10] = { 1,2,3,4,5,6,7,8,9,10 ; This will be an infinite loop because the unsigned int i; counter i is unsigned. The loop condition is for (i = 9; i >= 0; i--) if i >= 0 then do body and subtract 1 from i { Subtracting 1 from an unsigned int with a printf("index %u ", i); printf("\n"); 03. (Hint: What is guaranteed to print?) int main() { int x = returns_int(); float f = returns_float(); double d = returns_double(); if(x == (int)(float) x) { printf("a"); value of 0 will overflow to UINT_MAX. if(x == (int)(double) x) { printf("b"); The following will always be printed... if(f == (float)(double) f) { printf("c"); BCF if(d == (double)(float) d) { printf("d"); if(x + x >= 0) { printf("e"); if(d * d >= 0.0) { printf("f"); if((d+f)-d == f) { printf("g"); return 0; Grading Guide: The first two are all or nothing. The third one, if they include in their answer A, D or G they must explain: A: The value of x must be small enough to fit into the 23 frac bits of a float. D: The value of f must be small enough to fit into the 54 frac bits of a double. G: The values of d and f must be relatively close in magnitude. In other words, d and f must not be wildly different in terms of the size of the numbers they represent. If they answer in this way for all three give them full credit. Otherwise take a point off for each incorrect answer.

Section 4: IEEE 754-20 points Consider the following 6-bit floating point representation based on the IEEE floating point format. There is a sign bit s in the most significant bit. The next three bits are the exp, with an exponent bias of 3. The last two bits are the frac. The rules are like those in the IEEE standard (normalized, denormalized, representation of 0, infinity, and NaN). We consider the floating point format to encode numbers in a form:...where M is the significand/mantissa and E is the exponent. Fill in missing entries in the table with the following instructions for each column: M 2 : the value of M using binary format M 10 : the value of M using decimal format E 10 : the value of E in decimal format V 10 : the numeric value represented by the binary sequence using decimal format. Fractions or rationals are fine for base V 10. Use the provided scrap paper for your work at the end of this packet, but enter all the answers into the table. Bit Vector M 2 M 10 E 10 V 10 011011 1.11 1.75 3 14 100100 1.00 1-2 -0.25 -or- -1/4 100000 0.00 0-2 -0 111111 N/A -or- NaN N/A -or- NaN N/A -or- NaN NaN 000011 0.11 0.75-2.1875 -or 3/16 Grading Guide: One point for each correctly filled cell.

Section 5: Number Systems - 10 points Fill in the empty boxes in the following table (use the space below for your work, but enter all final answers into the table). (Hint: You can only use 8-bits!) Two's Complement 8-bit Binary Number Decimal 00001010 10 10101010-86 11011111-33 Unsigned 8-bit Binary Number Hexadecimal 11101010 OxEA Not Possible -or- N/A ^^^ If they wrote the 10110000 binary number there must be some sort of note that they understood that the MSB bit was truncated. In this case give half credit. 0x1B0 Grading Guide: Two points for each correctly filled cell.

Section 6: C Programming - 20 points In lab1, we searched for, inserted and removed nodes on a linked list. Here we will add a new function to our linked list library. In this question we will write a function with the following prototype: struct node* list_reduce(struct node* head); The purpose of this function will be to reduce the nodes of the linked list into one new list with one new node. In your program you can use the following linked list node struct, assume it is declared and defined. struct node { int value; struct node* next; So for example, imagine you had a linked list that looked like this in memory, where the head is the node with the value 1: The list returned would look like this (The return value is a pointer to the node with the value 10): Notes: You not have to write a whole program, no main function required. Do not mutate any existing nodes in the list passed as a parameter. Manage your memory properly. Put your answer on the following, lined page.

struct node* list_reduce(struct node* head) { int sum = 0; struct node* n = head; while(n!= NULL) { sum += n->value; struct node* freeme = n; n = n->next; free(freeme); struct node* result = malloc(sizeof(struct node)); result->value = sum; result->next = NULL; return result; Grading Guide: Additive points For each of the below, award the points. 3 pt using malloc properly to create a new node for the new list 2 pt looping over the old list properly 1 pt accessing and populating new struct properly 1 pt summing the old list properly 2 pt freeing the old list properly 10 pt general flow of program. (At a 10k ft level, is the code attempting to do the right things. Does their answer indicate an understanding of the problem and what is required for a solution? Moreover, 5 pts for writing any reasonable C code, then 5 more pts if the code demonstrates conceptual understanding of the problem and the steps required to get there.) Note that they don t have to have the exact same answer as me, just something approximately close.