As stated earlier, the declaration

Similar documents
Number of decimal places of precision has reduced! We supplied a value correct to 12 decimal places

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,...

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

More about Binary 9/6/2016

Computer System and programming in C

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

UNIT 7A Data Representation: Numbers and Text. Digital Data

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

Number System. Introduction. Decimal Numbers

Programming in C++ 5. Integral data types

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

(T) x. Casts. A cast converts the value held in variable x to type T

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Computer Systems Programming. Practice Midterm. Name:

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

COMP2611: Computer Organization. Data Representation

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

MC1601 Computer Organization

Part 2,Number Systems Questions

CS102: Variables and Expressions

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

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

CS 261 Fall Mike Lam, Professor Integer Encodings

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Number Systems. Both numbers are positive

Chapter 3. Fundamental Data Types

Data Representation in Computer Memory

LAB A Translating Data to Binary

Computers Programming Course 6. Iulian Năstac

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

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

Fundamental of Programming (C)

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Introduction to Computers and Programming. Numeric Values

World Inside a Computer is Binary

1.3b Type Conversion

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

Programming in C++ 6. Floating point data types

Data Types. Data Types. Integer Types. Signed Integers

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

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

Fundamentals of Programming

Expressions and Casting

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

Integer Representation

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

M1 Computers and Data

CS Programming In C

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

BSM540 Basics of C Language

First of all, it is a variable, just like other variables you studied

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Work relative to other classes

ENGR101: Lecture 7. C language tools and intro to C programming. March ENGR101: Lecture 7 March / 20

Getting started with Java

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

Fundamentals of Programming Session 2

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Run time environment of a MIPS program

Fundamentals of C Programming

More Programming Constructs -- Introduction

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Pointers. 10/5/07 Pointers 1

Preview from Notesale.co.uk Page 6 of 52

Learning to Program with Haiku

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Numerical Precision. Or, why my numbers aren t numbering right. 1 of 15

Chapter 3: Arithmetic for Computers

SIGNED AND UNSIGNED SYSTEMS

Course Outline Introduction to C-Programming

CSC201, SECTION 002, Fall 2000: Homework Assignment #2

Week 3 Lecture 2. Types Constants and Variables

Syntax and Variables

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

Fundamentals of Programming Session 8

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Floating Point Arithmetic

Arithmetic Expressions in C

Final Labs and Tutors

Unit 4. Input/Output Functions

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

This is CS50. Harvard University Fall Quiz 0 Answer Key

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

THE FUNDAMENTAL DATA TYPES

Fundamentals of Programming

More about BOOLEAN issues

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CS & IT Conversions. Magnitude 10,000 1,

Transcription:

The int data type As stated earlier, the declaration int a; is an instruction to the compiler to reserve a certain amount of memory to hold the values of the variable a. How much memory? Two bytes (usually, but not always, 16 bits) A bit is either 0 or 1 So the computer sets aside 16 places, each of which can store either 0 or 1 CKR Class Notes in C 55 So the computer sets aside 16 places each of which looks like the following. 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 1 This bit pattern corresponds to the binary representation which the computer uses to represent numbers internally. The number 40000 is too large to be represented in this way. But by what logic did the computer arrive at the figure 20000 + 20000 = -25536? CKR Class Notes in C 56

The binary representation Consider, first, the representation of a number with only 4 bits available. The bit pattern a 3 a 2 a 1 a 0 where each a i is either 0 or 1 can be interpreted as the number a 3 2 3 a 2 2 2 a 1 2 1 a 0 2 0 Thus, a 0 0, a 1 1, a 1 1, a 0 1, or the bit pattern 0111, corresponds to the number 0+4+2+1 = 7 CKR Class Notes in C 57 Limitations of the binary representation with fixed width (1) This method can represent only non-negative integers. In C-language, non-negative integers are represented by the data type unsigned int. (2) The maximum integer that can be represented with 4 bits is 2 3 2 2 2 1 2 0 15 CKR Class Notes in C 58

(3) The maximum integer that can be represented with n bits is 2 n 1 2 n 2 2 n 3 2 0 2 n 1 (The sum is obtained by summing the geometric series or geometric progression.) Hence, the maximum integer that can be represented with 16 bits is 65535. This is the maximum value for the unsigned data type. CKR Class Notes in C 59 Sign-magnitude representation How are negative numbers represented? One way is to reserve one bit for the sign of the number. This is called the sign bit. Then all numbers beginning with a 1 are regarded as negative. positive numbers are expressed as before. Thus, 1000 0001 is the representation of -1 CKR Class Notes in C 60

Difficulties with the sign-nagnitude representation What is 1000 0000? This is negative zero! Positive zero is still 0000 0000 Consider the sum 0000 0001 (+1) + 1000 0001 (-1) - 1000 0010 (-2) - Thus, we now have the conclusion: 1+ (-1) = -2 CKR Class Notes in C 61 One s complement A second method is to use the one s complement for negative numbers. The one s complement of a bit pattern is obtained by replacing all 0 s by 1 s and 1 s by 0 s. E.g. Number 0 1 0 1 1 c 1 0 1 0 Then all numbers beginning with a 1 are regarded as negative. a is represented by 1 c of a (for positive a). CKR Class Notes in C 62

Difficulties with one s complement With 8 bits, the one s complement representation of -38 is obtained as follows. First obtain the binary representation of 38 38 = 0010 0110 (= 32+4+2) Take the 1 c of this bit pattern to obtain -38 = 1101 1001. If we add 38 + (-38) we must get 38 + (-38) = 1111 1111 This is the 1 c of 0000 0000, so it should be something like negative zero. CKR Class Notes in C 63 But we also have the sum 0000 0000 (+0) - 0000 0001 -(+1) - 1111 1111 - which is to be understood like the sum 1 0000 0000-0000 0001-9999 9999 in the decimal representation, since 1 plays the role of both 1 and 9 in the binary representation. CKR Class Notes in C 64

Thus, we have the difficulty that 0-1 = 38-38 CKR Class Notes in C 65 Two s complement To correct this problem, we use the two s complement for negative numbers. To get the 2 c of a number Take the 1 c Add 1 to it. This makes the above sum come out correct. CKR Class Notes in C 66

1111 1110 (1 c representation of -1) becomes 1111 1111 (2 c representation of -1) so that 0 1 1 CKR Class Notes in C 67 Difficulties with the two s complement But we now have a new problem. Consider the sum 127 + 1 (using 8 bits) 0111 1111 (127) + 0000 0001 (1) - 1000 0000 (2 c representation of 128) - Thus, 127 1 128 CKR Class Notes in C 68

Solving the puzzle Extending the above logic to 16 bits, we get 32767 1 32768 A graphical way to describe this would be that the computer uses a number circle instead of a number line! CKR Class Notes in C 69 Solving the puzzle (contd) Thus, the computer adds correctly up to 32767 Thereafter, it wraps around. Since, 40000-32767 = 7533 we get 20000 20000 32768 7532 25536 We can easily verify the above considerations, by means of a simple program. CKR Class Notes in C 70

Checking the calculations /*Program Maxint.c*/ /*Function: To show the maximum value of int, and the wrap around property*/ #include <values.h> #include <stdio.h> #include <conio.h> main() { printf ( \n Maximum int = %d, MAXINT); printf ( \n Max int + 1 = %d, MAXINT+1); getch(); return 0; } CKR Class Notes in C 71 Output: Maximum int: 32767 Max int + 1 = -32768 CKR Class Notes in C 72

Checking that 2 bytes is NOT always 16 bits In the above program, we #included values.h, since this is the header file where the value MAXINT is defined. Why is it necessary to have a special constant for this? An int is 2 bytes, but not necessarily 16 bits. On a Win 32 platform, an int is 32 bits. We can check this out by running the Addint.c program in Visual C++. CKR Class Notes in C 73 We now get 20000 + 20000 = 40000 But the basic principles remain the same, as we can check by adding enough zeros after 2. CKR Class Notes in C 74

Note: Using the Visual C++ IDE This assumes that you have access to Visual C++. This refers to Visual C++ 6.0 But the same thing will work with any version of Visual C++. Step 1: Start the Visual C++ IDE. Step 2: Click File New Select Win 32 Console Application Select Create an Empty Project Step 3: Click Project Add to Project Files (or New if you want to key in the code directly). CKR Class Notes in C 75 To compile and run, select Build Run, and click OK. (Or use other choices under the Build option, if there are mistakes in the code. ) CKR Class Notes in C 76

The long modifier The same sort of effect can be achieved in Turbo C, by using the modifier long. This allows 32 bits of storage (in Turbo C) and changes the above values. We now have 2147483647 + 1 = - 2147483648 But there is no change in the principle of representing or adding numbers. CKR Class Notes in C 77 The modifiers long and unsigned can be used together. unsigned a; long b; unsigned long int c; are all valid declarations. The order in which the modifiers are used does not matter. There is no difference between unsigned long c; and long unsigned c; CKR Class Notes in C 78

For unsigned long we have the equation 4294967295 + 1 = 0 Obviously, long short int d; will generate the compiler error too many types in declaration. IMPORTANT NOTE: C99 permits the data type long long CKR Class Notes in C 79 Fundamental data types II: floats Is this the best we can do in computer arithmetic? We can improve matters by using the floating point data type. The integer data type cannot also be used where fractions are involved. For fractions, one uses the float data type, declared as follows: float a, b, c; CKR Class Notes in C 80