0% found this document useful (0 votes)
7 views6 pages

Lecture Slides 02 024-Shifting

The document discusses the representation and operations of integer and floating-point numbers in C, including unsigned and signed integers, arithmetic, shifting, and sign extension. It explains left and right shift operations for both unsigned and signed integers, as well as the extraction of specific bits using shifts and masks. Additionally, it covers the concept of sign extension when converting between different integer data types.

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)
7 views6 pages

Lecture Slides 02 024-Shifting

The document discusses the representation and operations of integer and floating-point numbers in C, including unsigned and signed integers, arithmetic, shifting, and sign extension. It explains left and right shift operations for both unsigned and signed integers, as well as the extraction of specific bits using shifts and masks. Additionally, it covers the concept of sign extension when converting between different integer data types.

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/ 6

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  
 

ShiBing  and  Sign  Extension  


University  of  Washington  

ShiB  Opera.ons  for  unsigned  integers  


¢ LeB  shiB:    x  <<  y   x   00000110  
§ Shi/  bit-­‐vector  x  le/  by  y  posi6ons  
<<  3   00110000  
00010000  
§ Throw  away  extra  bits  on  le/  
§ Fill  with  0s  on  right   >>  2   00011000  
00000001   1
¢ Right  shiB:    x  >>  y  
§ Shi/  bit-­‐vector  x  right  by  y  posi6ons  
§ Throw  away  extra  bits  on  right  
§ Fill  with  0s  on  le/   x   11110010  
<<  3   10010000  
00010000   144

>>  2   00111100  
00101000   60  s

ShiBing  and  Sign  Extension  


University  of  Washington  

ShiB  Opera.ons  for  signed  integers  


¢ LeB  shiB:    x  <<  y   x   01100010  
§ Equivalent  to  mul6plying  by  2y    
<<  3   00010000   16  s
§ (if  resul6ng  value  fits,  no  1s  are  lost)  
¢ Right  shiB:    x  >>  y   Logical  >>  2   00011000   24  
§ Logical  shi/  (for  unsigned  values)   Arithme6c  >>  2   00011000   24  
Fill  with  0s  on  le/  
§
§ Arithme6c  shi/  (for  signed  values)  
§ Replicate  most  significant  bit  on  le/   x   10100010  
§ Maintains  sign  of  x   <<  3   00010000   16  s
§ Equivalent  to  dividing  by  2y   40  s
Logical  >>  2   00101000  
§ Correct  rounding  (towards  0)  requires    
some  care  with  signed  numbers   Arithme6c  >>  2   11101000   -­‐24

Undefined behavior when


y < 0 or y ≥ word_size
ShiBing  and  Sign  Extension  
University  of  Washington  

Using  ShiBs  and  Masks  


¢ Extract  the  2nd  most  significant  byte  of  an  integer:  
§ First  shi/,  then  mask:  (  x  >>  16  )  &  0xFF  
x   01100001  01100010  01100011  01100100    
x  >>  16   00000000  00000000  
00010000  
01100001  01100010    
00011000  
00000000  00000000  00000000  11111111  
(  x  >>  16)  &  0xFF  
00000000  00000000  00000000  01100010    

¢ Extract  the  sign  bit  of  a  signed  integer:  


§ (  x  >>  31  )  &  1      -­‐  need  the  “&  1”  to  clear  out  all  other  bits  except  LSB  
¢ Condi.onals  as  Boolean  expressions  (assuming  x  is  0  or  1)  
§ if  (x)  a=y  else  a=z;        which  is  the  same  as            a  =  x  ?  y  :  z;  
§ Can  be  re-­‐wriaen  (assuming  arithme6c  right  shi/)  as:  
           a  =  (  (x  <<  31)  >>  31)  &  y  +  ((!x)  <<  31  )  >>  31  )  &  z;  
ShiBing  and  Sign  Extension  
University  of  Washington  

Sign  Extension  
¢ Task:  
§ Given  w-­‐bit  signed  integer  x  
§ Convert  it  to  w+k-­‐bit  integer  with  same  value  
¢ Rule:  
§ Make  k  copies  of  sign  bit:  
§ X  ʹ′  =    xw–1  ,…,  xw–1  ,  xw–1  ,  xw–2  ,…,  x0  

k  copies  of  MSB   w  


X
•••

•••

X ʹ′
••• •••
k   w  
ShiBing  and  Sign  Extension  
University  of  Washington  

Sign  Extension  Example  


¢ Conver.ng  from  smaller  to  larger  integer  data  type  
¢ C  automa.cally  performs  sign  extension  

short int x = 12345;


int ix = (int) x;
short int y = -12345;
int iy = (int) y;

Decimal Hex Binary


x 12345 30 39 00110000 01101101
ix 12345 00 00 30 39 00000000 00000000 00110000 01101101
y -12345 CF C7 11001111 11000111
iy -12345 FF FF CF C7 11111111 11111111 11001111 11000111

ShiBing  and  Sign  Extension  

You might also like