0% found this document useful (0 votes)
4 views5 pages

lecture_slides_02_023-integersC

The document discusses the representation of integers and floating-point numbers in C, including signed and unsigned integers, arithmetic operations, and the IEEE floating-point standard. It provides details on maximum and minimum values for different word sizes and the implications of casting between signed and unsigned types. Additionally, it highlights potential surprises in expression evaluation when mixing signed and unsigned values.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

lecture_slides_02_023-integersC

The document discusses the representation of integers and floating-point numbers in C, including signed and unsigned integers, arithmetic operations, and the IEEE floating-point standard. It provides details on maximum and minimum values for different word sizes and the implications of casting between signed and unsigned types. Additionally, it highlights potential surprises in expression evaluation when mixing signed and unsigned values.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

University

 of  Washington  

Sec.on  2:  Integer  &  Floa.ng  Point  Numbers  


¢ Representa.on  of  integers:  unsigned  and  signed  
¢ Unsigned  and  signed  integers  in  C  
¢ Arithme.c  and  shiBing  
¢ Sign  extension  

¢ Background:  frac.onal  binary  numbers  


¢ IEEE  floa.ng-­‐point  standard  
¢ Floa.ng-­‐point  opera.ons  and  rounding  
¢ Floa.ng-­‐point  in  C  
 

Integers  in  C  
University  of  Washington  

Values  for  Different  Word  Sizes  


  W  
  8   16   32   64  
UMax   255   65,535   4,294,967,295   18,446,744,073,709,551,615  
TMax   127   32,767   2,147,483,647   9,223,372,036,854,775,807  
TMin   -­‐128   -­‐32,768   -­‐2,147,483,648   -­‐9,223,372,036,854,775,808  
 
 

¢ Observa.ons   ¢ C  Programming  
§ |TMin  |    =    TMax  +  1   § #include  <limits.h>  
Asymmetric  range  
§ § Declares  constants,  e.g.:  
§ UMax  =  2  *  TMax  +  1   § ULONG_MAX  
§ LONG_MAX  
§ LONG_MIN  
§ Values  are  plaGorm  specific  
§ See:  /usr/include/limits.h  on  
Linux  
Integers  in  C  
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  
University  of  Washington  

Signed  vs.  Unsigned  in  C  


¢ Cas.ng  
§int tx, ty;
§ unsigned ux, uy;
§ Explicit  casXng  between  signed  &  unsigned:  
§ tx = (int) ux;
§ uy = (unsigned) ty;
§ Implicit  casXng  also  occurs  via  assignments  and  funcXon  calls:  
§ tx = ux;
§ uy = ty;  
§ The  gcc  flag  -­‐Wsign-­‐conversion  produces  warnings  for  implicit  casts,  
but  -­‐Wall  does  not!  
§ How  does  casXng  between  signed  and  unsigned  work  –  what  values  are  
going  to  be  produced?  
§ Bits  are  unchanged,  just  interpreted  differently!  

Integers  in  C  
University  of  Washington  

Cas.ng  Surprises  
¢ Expression  Evalua.on  
§ If  you  mix  unsigned  and  signed  in  a  single  expression,  then  
signed  values  implicitly  cast  to  unsigned  
§ Including  comparison  operaXons  <,  >,  ==,  <=,  >=
§ Examples  for  W  =  32:        TMIN  =  -­‐2,147,483,648              TMAX  =  2,147,483,647  
¢ Constant1  Constant2  Rela.on  Evalua.on  
 0 0  0U 0U     == unsigned
 -­‐1-1  0 0     < signed
 -­‐1-1  0U 0U     > unsigned
 2147483647
2147483647  -­‐2147483648  
-2147483648     > signed  
 2147483647U
2147483647U  -­‐2147483648
-2147483648     < unsigned
 -­‐1-1  -­‐2  -2     > signed
 (unsigned)-­‐1
(unsigned) -1  -­‐2  -2     > unsigned
   2147483647  
2147483647  2147483648U  
2147483648U     < unsigned
   2147483647  
2147483647  (int)  
(int)2147483648U  
2147483648U > signed
Integers  in  C  

You might also like