Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Simple Data Types in C - Introduction to Computer Systems - Lecture Slides, Slides of Computer System Design and Architecture

This course Introduction to Computer Systems consists of topics like arrays, pointers, concurrency, structures, unions and other concepts related to C programming. This lecture keywords are: Simple Data Types in C, Program Data Types, Data Types in C, Ascii Encoding, Integers, Fundamental Problem, Limit Number Range and Precision, Integer Representations, Integer Ranges, Detecting Overflow in Programs

Typology: Slides

2012/2013

Uploaded on 09/28/2013

noob
noob 🇮🇳

4.4

(25)

109 documents

1 / 40

Toggle sidebar

Related documents


Partial preview of the text

Download Simple Data Types in C - Introduction to Computer Systems - Lecture Slides and more Slides Computer System Design and Architecture in PDF only on Docsity! Simple Data Types in C docsity.com Objectives Be able to explain to others what a data type is Be able to use basic data types in C programs Be able to see the inaccuracies and limitations introduced by machine representations of numbers docsity.com Everything is Just a Bunch of Bits Bits can represent many different things  Depends on interpretation You and your program must keep track of what kind of data is at each location in the computer’s memory  E.g., program data types docsity.com Big Picture Processor works with finite-sized data All data implemented as a sequence of bits  Bit = 0 or 1  Represents the level of an electrical charge Byte = 8 bits Word = largest data size handled by processor  32 bits on most older computers  64 bits on most new computers docsity.com Data types in C Only really four basic types:  char  int (short, long, long long, unsigned)  float  double Size of these types on CLEAR machines: Sizes of these types vary from one machine to another! Type Size (bytes) char 1 int 4 short 2 long 8 long long 8 float 4 double 8 docsity.com Characters are just numbers What does this function do? char fun(char c) { char new_c; if ((c >= ’A’) && (c <= ’Z’)) new_c = c - ’A’ + ’a’; else new_c = c; return (new_c); } return type procedure name argument type and name local variable type and name Math on characters! comparisons with characters! docsity.com Integers Fundamental problem:  Fixed-size representation can’t encode all numbers Standard low-level solution:  Limit number range and precision • Usually sufficient • Potential source of bugs Signed and unsigned variants  unsigned modifier can be used with any sized integer (short, long, or long long) docsity.com Signed Integer +2. S2%T... 52,768. sap "32.767 2. -32. 16... docsity.com Integer Ranges Unsigned UMinn … UMaxn = 0 … 2 n-1: 32 bits: 0 ... 4,294,967,295 unsigned int 64 bits: 0 ... 18,446,744,073,709,551,615 unsigned long int 2’s Complement TMinn … TMaxn = -2 n-1 … 2n-1-1: 32 bits: -2,147,483,648 ... 2,147,483,647 int 64 bits: -9,223,372,036,854,775,808 … 9,223,372,036,854,775,807 long int Note: C numeric ranges are platform dependent! #include <limits.h> to define ULONG_MAX, UINT_MIN, INT_MAX, … docsity.com Detecting Overflow in Programs Some high-level languages (ML, Ada, …):  Overflow causes exception that can be handled C:  Overflow causes no special event  Programmer must check, if desired E.g., given a, b, and c=UAddn(a,b) – overflow? Claim: Overflow iff c < a (Or similarly, iff c < b) Proof: Know 0  b < 2n If no overflow, c = (a + b) mod 2n = a + b  a + 0 = a If overflow, c = (a + b) mod 2n = a + b – 2n < a docsity.com Overflow z is 951,946,282 not 5,246,913,578 as expected (compiler warned me, though, because they are constants) y is not a valid positive number (sign bit is set)! It’s -1,171,510,507 z is still 951,946,282 unsigned int x = 2123456789; unsigned int y = 3123456789; unsigned int z; z = x + y; int x = 2123456789; int y = 3123456789; int z; z = x + y; docsity.com Bit Shifting for Multiplication/Division Why useful?  Simpler, thus faster, than general multiplication & division  Standard compiler optimization Can shift multiple positions at once:  Multiplies or divides by corresponding power-of-2  a << 5 a >> 5 docsity.com A Sampling of Integer Properties Mostly as usual, e.g.: 0 is identity for +, - 1 is identity for ×, ÷ +, -, × are associative +, × are commutative × distributes over +, - Some surprises, e.g.: ÷ doesn’t distribute over +, -  (a,b > 0  a + b > a) Why should you care? – Programmer should be aware of behavior of their programs – Compiler uses such properties in optimizations For both unsigned & 2’s complement: docsity.com Beware of Sign Conversions in C Beware implicit or explicit conversions between unsigned and signed representations! One of many common mistakes: Always false(!) because -1 is converted to unsigned, yielding UMaxn unsigned int u; … if (u > -1) … ? What’s wrong? ? docsity.com FP Overflow & Underflow Fixed-sized representation leads to limitations Expressible negative values Expressible positive values Negative underflow Positive underflow Positive overflow Negative overflow Zero Large positive exponent. Unlike integer arithmetic, overflow  imprecise result (), not inaccurate result Round to + Round to - Large negative exponent Round to zero docsity.com FP Representation Fixed-size representation  Using more significand bits  increased precision  Using more exponent bits  increased range Typically, fixed # of bits for each part, for simplicity 1.001101110 × 25 significand exponent docsity.com FP Representation: IEEE 754 Current standard version of floating-point Single-precision (float) One word: 1 sign bit, 23 bit fraction, 8 bit exponent Positive range: 1.17549435 × 10-38 … 3.40282347 × 10+38 Double-precision (double) Two words: 1 sign bit, 52 bit fraction, 11 bit exponent Positive range: 2.2250738585072014 × 10-308 … 1.7976931348623157 × 10+308 Representation details covered in ELEC 220 Lots of details in B&O Chapter 2.4 docsity.com FP vs. Integer Results int i = 1000 / 6; float f = 1000.0 / 6.0; True mathematical answer: 1000  6 = 166 2/3 i = ? f = ? 166 166.666672 Integer division ignores remainder FP arithmetic rounds result Surprise! Arithmetic in binary, printing in decimal – doesn’t always give expected result docsity.com FP  Integer Conversions in C int i = 3.3 * 5; float f = i; True mathematical answer: 3.3  5 = 16 ½ i = ? f = ? 16 16.0 Converts 5  5.0 – Truncates result 16 ½  16 integer  FP: Can lose precision Rounds, if necessary 32-bit int fits in double-precision FP FP  integer: Truncate fraction If out of range, undefined – not error docsity.com FP Behavior Programmer must be aware of accuracy limitations! Dealing with this is a main subject of CAAM 353 (1010 + 1030) + –1030 =? 1010 + (1030 + –1030) 1030 – 1030 =? 1010 + 0 0  1010 Operations not associative! (1.0 + 6.0) ÷ 640.0 =? (1.0 ÷ 640.0) + (6.0 ÷ 640.0) 7.0 ÷ 640.0 =? .001563 + .009375 .010937  .010938 ×,÷ not distributive across +,- docsity.com Booleans in C bool added to C in 1999 Many programmers had already defined their own Boolean type  To avoid conflict bool is disabled by default #include <stdbool.h> bool bool1 = true; bool bool2 = false; Important! Compiler needs this or it won't know about "bool"! docsity.com C’s Common Boolean Operations C extends definitions to integers  Booleans are encoded as integers • 0 == false • non-0 == true  Logical AND: 0 && 4 == 0 3 && 4 == 1 3 && 0 == 0  Logical OR: 0 || 4 == 1 3 || 4 == 1 3 || 0 == 1  Logical NOT: ! 4 == 0 ! 0 == 1 && and || short-circuit  Evaluate 2nd argument only if necessary  E.g., 0 && error-producing-code == 0 docsity.com Enumerated Types E.g., a Color = red, blue, black, or yellow  Small (finite) number of choices  Booleans & characters are common special cases  Pick arbitrary bit patterns for each Not enforced in C  Actually just integers  Can assign values outside of the enumeration  Could cause bugs docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved