03 Integers in C

background image

University of Washington

Section 2: Integer & Floating

Point Numbers

Representation of integers: unsigned and
signed

Unsigned and signed integers in C

Arithmetic and shifting

Sign extension

Background: fractional binary numbers

IEEE floating-point standard

Floating-point operations and rounding

Floating-point in C

Integers in C

background image

University of Washington

C Programming

#include <limits.h>

Declares constants, e.g.:

ULONG_MAX

LONG_MAX

LONG_MIN

Values are platform
specific

See: /usr/include/limits.h
on Linux

Values for Different Word Sizes

Integers in C

Observations

|TMin |

= TMax +

1

Asymmetric range

UMax

= 2 *

TMax + 1

background image

University of Washington

Signed vs. Unsigned in C

Constants

By default are considered to be signed integers

Use “U” suffix to force unsigned:

0U, 4294967259U

Integers in C

background image

University of Washington

Signed vs. Unsigned in C

Casting

int tx, ty;

unsigned ux, uy;

Explicit casting between signed & unsigned:

tx = (int) ux;

uy = (unsigned) ty;

Implicit casting also occurs via assignments and function
calls:

tx = ux;

uy = ty;

The gcc flag

-Wsign-conversion

produces warnings for

implicit casts, but

-Wall

does not!

How does casting between signed and unsigned work –
what values are going to be produced?

Bits are unchanged

, just interpreted differently!

Integers in C

background image

University of Washington

0 0U

==unsigned

-1 0

< signed

-1 0U

> unsigned

2147483647 -2147483648

> signed

2147483647U

-2147483648

< unsigned

-1 -2

> signed

(unsigned) -1 -2

> unsigned

2147483647 2147483648U

< unsigned

2147483647 (int) 2147483648U

> signed

Casting Surprises

Integers in C

Expression Evaluation

If you mix unsigned and signed in a single expression, then

signed values implicitly cast to unsigned

Including comparison operations <, >, ==, <=, >=

Examples for W = 32:

TMIN = -2,147,483,648

TMAX = 2,147,483,647

Constant

1

Constant

2

Relation

Evaluation

0 0U
-1 0
-1 0U
2147483647 -2147483648
2147483647U

-2147483648

-1 -2
(unsigned)-1 -2
2147483647 2147483648U
2147483647 (int) 2147483648U


Document Outline


Wyszukiwarka

Podobne podstrony:
03 Integers in C
03 3 sitowa i in
03 Galaxy in Flames
MacGregor, Kinley (aka Sherrilyn Kenyon) Brotherhood 03 Born in Sin rtf
Diana Palmer Men of Medicine Ridge 03 Diamond in the Rough
Bova, Ben Orion 03 Orion in the Dying Time
Stasheff, Christopher Rogue Wizard 03 A Wizard in War
Nora Roberts Concannon Sisters Trilogy 03 Born In Shame
Evan Innes America 2040 Book 03 City In The Mist
Gardner, Craig Shaw Ebenezum 03 A Night in the Netherhells
Baker, Kage Company 03 Mendoza in Hollywood 1 0
MacAlister 03 Born in Sin
Norton, Andre Free Trader Moon Singer 03 Flight in Yiktor
Eliza Knight [Highland Jewel 03] Love In A Box ([df)(1)
Hugh Zachary America 2040 03 City In the Mist
MacGregor, Kinley (aka Sherrilyn Kenyon) MacAlister 03 Born in Sin
Roberts, Nora Born 03 Born In Shame

więcej podobnych podstron