Chapter 3 Basic Data Types. Lecture 3 1

Similar documents
BSM540 Basics of C Language

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

Beginning C Programming for Engineers

Data Types. Data Types. Integer Types. Signed Integers

Number System. Introduction. Decimal Numbers

CSCI 2212: Intermediate Programming / C Chapter 15

CS & IT Conversions. Magnitude 10,000 1,

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Computer Organization & Systems Exam I Example Questions

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Advanced C Programming Topics

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

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

Signed Binary Numbers

ANSI C Programming Simple Programs

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

Lecture 02 C FUNDAMENTALS

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Work relative to other classes

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

Reserved Words and Identifiers

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

THE FUNDAMENTAL DATA TYPES

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Arithmetic Operators. Portability: Printing Numbers

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

CS61B Lecture #14: Integers

Lecture 3. More About C

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

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

Binary Representations and Arithmetic

CMPE223/CMSE222 Digital Logic Design. Positional representation

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

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

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

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

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

UNIT- 3 Introduction to C++

Chapter 3: Operators, Expressions and Type Conversion

Engineering Computing I

UNIT 3 OPERATORS. [Marks- 12]

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

Unit 4. Input/Output Functions

Fundamental of C programming. - Ompal Singh

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

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Number Systems, Scalar Types, and Input and Output

CSC 1107: Structured Programming

Number representations

Chapter 7. Basic Types

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

LECTURE 3 C++ Basics Part 2

COMP2611: Computer Organization. Data Representation

Level ISA3: Information Representation

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Fundamentals of Programming

CS33 Project Gear Up. Data

Computer System and programming in C

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

The Design of C: A Rational Reconstruction

C-string format with scanf/printf

Informatics Ingeniería en Electrónica y Automática Industrial

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

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

A flow chart is a graphical or symbolic representation of a process.

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

The Design of C: A Rational Reconstruction"

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

A Fast Review of C Essentials Part I

Introduction to Computers and Programming. Numeric Values

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Computers Programming Course 5. Iulian Năstac

Introduction to Computer Science-103. Midterm

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

3. Types of Algorithmic and Program Instructions

Basic Types and Formatted I/O

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

BSM540 Basics of C Language

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

LAB A Translating Data to Binary

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Lecture 13 Bit Operations

Data Representation 1

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

DEPARTMENT OF MATHS, MJ COLLEGE

T02 Tutorial Slides for Week 2

COMP Primitive and Class Types. Yi Hong May 14, 2015

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

1.3b Type Conversion

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Transcription:

Chapter 3 Basic Data Types Lecture 3 1

Topics Scalar Types in C Integers Bit Operations Floating Point Types Conversions Lecture 4 2

Scalar Types in C The amount of memory available for a variable depends on its type. Each type as a specific size in the memory of a computer. Each type has a specific format in the memory of a computer. A Scalar Type contains a single memory value. Lecture 4 3

Computer Memory Computers store two things in memory: The executable code and the data values A bit is a memory area that contains either a single value of 0 or a single value of 1. A byte consists of 8 bits of memory. A word is a set of one or more bytes Lecture 4 4

Integers and Integer Bases An integer (int) is a whole number, ranging from the negative to positive range....,-n,-n+1,-n+2,...,-2,-1,0,1,...,n-1,n-2,n,... An integer is stored as a bit pattern in binary notation. Lecture 4 5

Integers and Integer Bases Base notation is how a number is represented when recorded. The decimal system uses a base ten notation. Lecture 4 6

Integers and Integer Bases Example of Base 10 (Decimal) One-hundred and twenty-six, 126, is a representation of a base 10 number. The right most column represents 10 0, or 1 The middle column represents 10 1, or 10 The leftmost column represents 10 2, or 100 So, 1*100+2*10+6*1 equals 126. Lecture 4 7

Integer and Integer Bases In computers, integers tend to be represented in three other bases. Binary represents the base 2 notation Octal represents the base 8 notation Hexadecimal represents base 16 notation Lecture 4 8

Integers and Integer Bases For Binary, each column represents a power of 2 The rightmost column is 2 0, or 1 The next column is 2 1, or 2 The next column is 2 2, or 4 The next column is 2 3 or 8 Each further column is 2 (n+1) What is 0101 binary in decimal? Lecture 4 9

Integers and Integer Bases For Octal, each column represents a power of 8 The rightmost column is 8 0, or 1 The next column is 8 1, or 8 The next column is 8 2, or 64 The next column is 8 3 or 512 Each further column is 8 (n+1) What is 1010 octal in decimal? Lecture 4 10

Integers and Integer Bases For Hexadecimal, each column represents a power of 16 The rightmost column is 16 0, or 1 The next column is 16 1, or 16 The next column is 16 2, or 256 The next column is 16 3 or 4096 Each further column is 16 (n+1) What is 1101 hexadecimal in decimal? Lecture 4 11

Integer and Integer Bases Decimal Binary Octal Hexadecimal 0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F Lecture 4 12 16 10000 20 10

The Integer Types An integer is usually of the size of one word on a computer. The size of word changes from machine to machine. A 16 bit machine has a range from -32768 to +32767 A 32 bit machine has a range from -2147483648 to + 2147483647 Lecture 4 13

Integer Types Three types of integers a regular integer usually takes up a word of space. a short integer, declared short or short int takes up less memory space a long integer, declared long or long int takes up more memory space Unsigned integer, declared unsigned int Lecture 4 14

Integer Types Typically, an int in a system is already the type of long or short. In Visual Studio, a short is a 2-byte word. In Visual Studio, a long is a 8-byte word. Which of these is type int in Visual Studio? Lecture 4 15

Input and Output of Integer Types For scanf and printf, the following format strings can be used for integer types long in decimal %ld long in octal %lo long in hexdecimal %lx or %lx short in decimal %hd short in octal %ho short in hexadecimal %hx or %hx Lecture 4 16

The Sizeof Operator The sizeof operater gives the number of bytes associated with a type or a variable. int x; mysize=sizeof(x); mysize=sizeof(long int); mysize would be the size of an integer mysize=sizeof(intvar+96); uses the memory space of an integer, it still has the size of an integer. Lecture 4 17

A Closer Look at Integers The size of the integer will differ from machine to machine. The method of storing an integer will be consistent from machine to machine. Lecture 4 18

Binary Representation of an Integer For the following, the size of an integer will be assumed to be 16 bits. The value of the integer will be represented in the lower 15 bits of the binary number. The first bit of the integer will represent the sign bit. Lecture 4 19

Excel Spreadsheet Example An integer uses the highest bit of its word to represent the sign of the value. A 16-bit integer will have a range of -2 15 to positive 2 15-1, which is -32768 to 32767. When the sign bit becomes active, with no subsequent bits, the value is the maximum negative number on this scale. Lecture 4 20

Integer Representation -1 0 1-2 2-32766 32766-32767 32767-32768 Lecture 4 21

Character Variables There are 128 characters in ANSI C from 0-127 inclusive Each of these characters can be treated as an integer. Each character can be represented by a single byte of 8 bits Lecture 4 22

Character Variables char Alpha; Alpha= A ; printf( Alpha is \ %c \ \n,alpha); printf( Alpha is %d \n,alpha); printf( Next letter is \ %c\ \n,alpha+1); Results in : Next letter is B Lecture 4 23

Unsigned Types Unsigned variables increase the range of the type by using the sign bit to store data. A 16 bit integer has a range of -32768 to 32767. A 16 bit unsigned integer has a range of 0 to 65535 Lecture 4 24

Bitwise Operations on an Integer ~ bitwise-negation & bitwise-and ^ bitwise-xor bitwise-or >> shift right << shift left Lecture 4 25

Binary Operators Bitwise Negation ~ int value = 242 ; Value in binary is 000000001111001 ~Value in binary is 111111110000110 printf("value is %d\n",value); printf("~value is %d\n",~value); The output would be: Value is 242 ~Value is -243 Lecture 4 26

Bitwise Operators Bitwise And & int FirstValue=242; int SecondValue=115; FirstValue in Binary is: 0000000011110010 SecondValue in Binary is: 0000000001110011 Bitwise AND compares each column of the binary form. If both are 1, then the result is 1. If not, the result is 0 Application: 0000000011110010 0000000001110011 0000000001110010 The resulting value as an integer becomes 114 Lecture 4 27

Bitwise Operators Bitwise Xor ^ XOR returns TRUE if the inputs are different. 0^0 is 0 0^1 is 1 1^0 is 1 1^1 IS 0 Lecture 4 28

Bitwise Operators Bitwise XOR ^ int FirstValue=242; int SecondValue=115; FirstValue in Binary is: 0000000011110010 SecondValue in Binary is: 0000000001110011 Bitwise XOR compares each column of the binary form. If both are the same, then the result is 0. If not, the result is 1. Application: 0000000011110010 0000000001110011 0000000010000001 The resulting value as an integer becomes 129 Lecture 4 29

Bitwise Operators Bitwise OR int FirstValue=242; int SecondValue=115; FirstValue in Binary is: 0000000011110010 SecondValue in Binary is: 0000000001110011 Bitwise OR compares each column of the binary form. If either one is 1, then the result is 1. If not, the result is 0 Application: 0000000011110010 0000000001110011 0000000011110011 The resulting value as an integer becomes 243 Lecture 4 30

Bitwise Operators >> right shift << left shift Moves the binary value left or right by the given value. Lecture 4 31

Bitwise Operators int FirstValue=144; int ValueLeftShift=0; int ValueRightShift=0; FirstValue in Binary is: 0000000010010000 Shift two positions to the right ValueRightShift=FirstValue>>2; The binary result is: 0000000000100100 The integer value is 36 ValueLeftShift=FirstValue<<2; The binary result is: 0000001001000000 The integer value is 576 Lecture 4 32

Floating Point Types Floating point numbers are more like regular arithmetic 9.81, 32.2 are examples of floating point numbers Floating point numbers are another type of variable. Lecture 4 33

Floating Point Types Declaration of Variables float Speed; double Acceleration=0.0; long double Pmantis=0.0; Assignment of Variables Speed=44.0f; Speed = 44; Acceleration=7.0; Pmantis=6.665L; Lecture 4 34

Floating Point Types Many, but not all, of the operators are the same for integers and floats +, -, *, / ==,!=, <, <=, >, >=!, &&, But NOT the remainder function % Lecture 4 35

Input and Output There are three floating point types float double long double Notations %f is for a decimal notation %e is for an exponential notation %g is for either decimal or exponential notation Format Strings for Floating Point Types %f, %e, %g for a float %lf, %le, %lg for a double %Lf, %Le, %Lg, for a long double Lecture 4 36

Types of Expressions and Automatic Conversions The result of any logical operator is an int value. Most conversions in C are automatic, from highest to least priority long double double float unsigned long long unsigned int int Lecture 4 37

Automatic Conversions Exceptions Any expression involving char, signed, unsigned char, short, or unsigned short, the dominating type will be either int or unsigned int Expressions of long int and unsigned int, the dominating type could be either long int or unsigned long int. Lecture 4 38

Automatic Type Conversions Examples 2+3.113 becomes a double of 5.113 a +1 becomes an integer 98, which can be formatted to the character b Lecture 4 39

Forced Type Conversions Many times an automatic conversion does not give the desired result. A type cast can be used to explicitly convert a value or the result of an expression to a known type. (type) expression is the usual format for the routine. Lecture 4 40

Force Type Conversions (float) 1 becomes 1.000000 y = ((float) 'a') + 1 becomes 98.000000 (int) 'B becomes 66 y = (int) 2.4 becomes 2 Lecture 4 41