+ All Categories
Home > Documents > 02 Bits Ints

02 Bits Ints

Date post: 14-Jan-2016
Category:
Upload: vikas-pandey
View: 222 times
Download: 0 times
Share this document with a friend
Description:
discussion about bits & integers in system software
79
 Carnegie Mellon 1 !"#$% !'#($% )*+ ,*#(-(.$ !"#$!%& ()*+,-./0,) *, 1,23.*4+ 567*427  $ )-  84/*.+49 :.;< $=9 $>!> ,*$#./0#1.$2 ?@)-6 A+6@)* @)- B@C4 DEF@GG@+,)
Transcript
Page 1: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 1/79

Carnegie Mellon

1

!"#$% !'#($% )*+ ,*#(-(.$

!"#$!%& ()*+,-./0,) *, 1,23.*4+ 567*427 $)- 84/*.+49 :.;< $=9 $>!>

,*$#./0#1.$2 

?@)-6 A+6@)* @)- B@C4 DEF@GG@+,)

Page 2: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 2/79

Carnegie Mellon

2

31+)'2 !"#$% !'#($% )*+ ,*#(-(.$

!  4(5.($(*6*- "*71.8)61* )$ 9"#$

!  !"#:;(<(; 8)*"5/;)61*$

!  ,*#(-(.$

!  ?43+474)*@0,)& .)7H;)4- @)- 7H;)4-

1,)C4+7H,)9 /@70);

!  IJ3@)-H);9 *+.)/@0);

!  :--H0,)9 )4;@0,)9 2.G03GH/@0,)9 7KHLH);

!  =/88).'

Page 3: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 3/79

Carnegie Mellon

3

!"*).' 4(5.($(*#)61*$

0.0V 

0.5V 

2.8V 

3.3V 

0  1  0 

Page 4: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 4/79

Carnegie Mellon

4

>*01+"*- !'#( ?);/($

!  !'#( @ A 9"#$

!  AH)@+6 >>>>>>>>$ *, !!!!!!!!$ 

!  B4/H2@G& >!> *, $""!> 

!  F4J@-4/H2@G >>!= *, MM!= 

A@74 != ).2N4+ +43+474)*@0,)

!  O74 /K@+@/*4+7 P>E *, PQE @)- P:E *, PME

!  R+H*4 M:!B%SA!= H) 1 @7

 T >JM:!B%SA

 T >JU@!-%SN

0 0 00001 1 00012 2 00103 3 0011

4 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011

C 12 1100D 13 1101E 14 1110F 15 1111

Page 5: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 5/79

Carnegie Mellon

5

!'#(:B."(*#(+ C(81.' B.-)*"D)61*

!  E.1-.)8$ 4(7(. #1 ?".#/); F++.($$($!  1,)/43*.@GG6 C4+6 G@+;4 @++@6 ,U N6*47

!  :/*.@GG6 H23G424)*4- VH*K KH4+@+/K6 ,U -HW4+4)* 242,+6 *6347

!  567*42 3+,CH-47 @--+477 73@/4 3+HC@*4 *, 3@+0/.G@+ X3+,/477Y

!  Z+,;+@2 N4H); 4J4/.*4-

Z+,;+@2 /@) /G,NN4+ H*7 ,V) -@*@9 N.* ),* *K@* ,U ,*K4+7

!  G185";(. H 4/*:3"8( ='$#(8 G1*#.1; F;;10)61*

!  RK4+4 -HW4+4)* 3+,;+@2 ,N[4/*7 7K,.G- N4 7*,+4-

!  :GG @GG,/@0,) VH*KH) 7H);G4 CH+*.@G @--+477 73@/4

• • • 

Page 6: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 6/79

Carnegie Mellon

6

C)0I"*( J1.+$

!  C)0I"*( K)$ LJ1.+ ="D(M

!  \,2H)@G 7H]4 ,U H)*4;4+#[email protected] -@*@

!  ()/G.-H); @--+47747

!  ^,7* /.++4)* 2@/KH)47 .74 %$ NH*7 _` N6*47a V,+-7

8H2H*7 @--+47747 *, `bA

!  A4/,2H); *,, 72@GG U,+ 242,+6#H)*4)7HC4 @33GH/@0,)7

!  FH;K#4)- 767*427 .74 =` NH*7 _c N6*47a V,+-7

!  Z,*4)0@G @--+477 73@/4 d !<c e !>!Q N6*47

!  Jc=#=` 2@/KH)47 7.33,+* `c#NH* @--+47747& $"= f4+@N6*47

^@/KH)47 7.33,+* 2.G03G4 -@*@ U,+2@*7

!  M+@/0,)7 ,+ 2.G03G47 ,U V,+- 7H]4

!  :GV@67 H)*4;+@G ).2N4+ ,U N6*47

Page 7: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 7/79

Carnegie Mellon

7

J1.+:B."(*#(+ C(81.' B.-)*"D)61*

!  F++.($$($ =5(0"7' !'#(

N10)61*$

!  :--+477 ,U g+7* N6*4 H) V,+-

!  :--+47747 ,U 7.//477HC4 V,+-7 -HW4+

N6 ` _%$#NH*a ,+ c _=`#NH*a

0000

0001

0002

0003

0004

0005

0006

0007

0008

00090010

0011

32-bit Words 

Bytes  Addr. 

0012

0013

0014

0015

64-bit Words 

Addr

??

Addr

??

Addr

??

Addr

??

Addr

??

Addr

??

0000

0004

0008

0012

0000

0008

Page 8: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 8/79

Carnegie Mellon

8

O)#) 4(5.($(*#)61*$

C Data Type Typical 32-bit Intel IA32 x86-64

char 1 1 1

short 2 2 2

int 4 4 4

long 4 4 8

long long 8 8 8

float 4 4 4

double 8 8 8

long double 8 10/12 10/16

pointer 4 4 8

Page 9: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 9/79

Carnegie Mellon

9

!'#( B.+(."*-

!  K1P $I1/;+ 9'#($ P"#I"* ) 8/;6:9'#( P1.+ 9( 1.+(.(+ "*

8(81.'Q

!  G1*<(*61*$

!  AH; I)-H@)& 5.)9 ZZ1 ^@/9 ()*4+)4*

84@7* 7H;)Hg/@)* N6*4 K@7 KH;K47* @--+477

!  8HhG4 I)-H@)& Jc=

!  84@7* 7H;)Hg/@)* N6*4 K@7 G,V47* @--+477

Page 10: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 10/79

Carnegie Mellon

10

!'#( B.+(."*- >R)85;(

!  !"- >*+")*

!  84@7* 7H;)Hg/@)* N6*4 K@7 KH;K47* @--+477

!  N"S;( >*+")*

!  84@7* 7H;)Hg/@)* N6*4 K@7 G,V47* @--+477

!  >R)85;(

!  i@+H@NG4 J K@7 `#N6*4 +43+474)*@0,) >J>!$%`"=S

!  :--+477 ;HC4) N6 jJ H7 >J!>>

0x100 0x101 0x102 0x10301 23 45 67

0x100 0x101 0x102 0x103

67 45 23 01

Big Endian 

Little Endian 

01 23 45 67

67 45 23 01

Page 11: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 11/79

Carnegie Mellon

11

Address   Instruction Code   Assembly Rendition 

8048365: 5b pop %ebx8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx

804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx)

4()+"*- !'#(:4(<(.$(+ N"$6*-$

!  O"$)$$(89;'

!  f4J* +43+474)*@0,) ,U NH)@+6 2@/KH)4 /,-4

!  b4)4+@*4- N6 3+,;+@2 *K@* +4@-7 *K4 2@/KH)4 /,-4

>R)85;( T.)-8(*#

!  O(0"5I(."*- U/89(.$

[email protected]& 0x12ab 

!  Z@- *, %$ NH*7& 0x000012ab 

!  53GH* H)*, N6*47& 00 00 12 ab 

!  ?4C4+74& ab 12 00 00 

Page 12: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 12/79

Carnegie Mellon

12

>R)8"*"*- O)#) 4(5.($(*#)61*$

!  G1+( #1 E."*# !'#( 4(5.($(*#)61* 17 O)#)

!  1@70); 3,H)*4+ *, .)7H;)4- /K@+ k /+4@*47 N6*4 @++@6

Printf directives: 

%p:   Print pointer 

%x:   Print Hexadecimal 

typedef unsigned char *pointer;

void show_bytes(pointer start, int len){int i;for (i = 0; i < len; i++) printf(”%p\t0x%.2x\n",start+i, start[i]);

 printf("\n");}

Page 13: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 13/79

Carnegie Mellon

13

show_bytes >R(0/61* >R)85;(

int a = 15213;

printf("int a = 15213;\n");

show_bytes((pointer) &a, sizeof(int));

Result (Linux): 

int a = 15213;

0x11ffffcb8 0x6d

0x11ffffcb9 0x3b

0x11ffffcba 0x00

0x11ffffcbb 0x00

Page 14: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 14/79

Carnegie Mellon

14

4(5.($(*6*- ,*#(-(.$Decimal:  15213

Binary:  0011 1011 0110 1101

Hex:  3 B 6 D

6D

3B00

00

IA32, x86-64 

3B

6D

00

00

Sun 

int A = 15213; 

93

C4

FF

FF

IA32, x86-64 

C4

93

FF

FF

Sun 

Two"s complement representation 

(Covered later) 

int B = -15213; 

long int C = 15213; 

00

00

0000

6D3B

00

00

x86-64 

3B

6D

0000

Sun 

6D3B

00

00

IA32 

Page 15: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 15/79

Carnegie Mellon

15

4(5.($(*6*- E1"*#(.$

BHW4+4)* /,23HG4+7 j 2@/KH)47 @77H;) -HW4+4)* G,/@0,)7 *, ,N[4/*7

int B = -15213;

int *P = &B;

x86-64 Sun  IA32 

EF 

FF 

FB 

2C 

D4 

F8 

FF 

BF 

0C 

89 

EC 

FF 

FF 

7F 

00 

00 

Page 16: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 16/79

Carnegie Mellon

16

4(5.($(*6*- =#."*-$

!  =#."*-$ "* G

!  ?43+474)*4- N6 @++@6 ,U /K@+@/*4+7

!  I@/K /K@+@/*4+ 4)/,-4- H) :51(( U,+2@*

!  5*@)-@+- S#NH* 4)/,-H); ,U /K@+@/*4+ 74*

1K@+@/*4+ X>Y K@7 /,-4 >J%>

 T BH;H* H K@7 /,-4 >J%>lH

!  5*+H); 7K,.G- N4 ).GG#*4+2H)@*4-

!  MH)@G /K@+@/*4+ m >

!  G185)69";"#'

A6*4 ,+-4+H); ),* @) H77.4

Linux/Alpha  Sun 

31 

38 

32 

34 

33 

00 

31 

38 

32 

34 

33 

00 

Page 17: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 17/79

Carnegie Mellon

17

31+)'2 !"#$% !'#($% )*+ ,*#(-(.$

!  4(5.($(*6*- "*71.8)61* )$ 9"#$

!  !"#:;(<(; 8)*"5/;)61*$

!  ,*#(-(.$

!  ?43+474)*@0,)& .)7H;)4- @)- 7H;)4-

1,)C4+7H,)9 /@70);

!  IJ3@)-H);9 *+.)/@0);

!  :--H0,)9 )4;@0,)9 2.G03GH/@0,)9 7KHLH);

!  =/88).'

Page 18: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 18/79

Carnegie Mellon

18

!11;()* F;-(9.)

!  O(<(;15(+ 9' V(1.-( !11;( "* WX#I G(*#/.'

!  :G;4N+@H/ +43+474)*@0,) ,U G,;H/

!  I)/,-4 Xf+.4Y @7 ! @)- XM@G74Y @7 >

:)-

" :jA m ! VK4) N,*K :m! @)- Am!

D+

" :nA m ! VK4) 4H*K4+ :m! ,+ Am!

\,*" o: m ! VK4) :m>

IJ/G.7HC4#D+ _e,+a" :pA m ! VK4) 4H*K4+ :m! ,+ Am!9 N.* ),* N,*K

Page 19: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 19/79

Carnegie Mellon

19

F55;"0)61* 17 !11;()* F;-(9.)

!  F55;"(+ #1 O"-"#); ='$#(8$ 9' G;)/+( =I)**1*

!  !Q%S ^(f ^@7*4+E7 fK47H7

!  ?4@7,) @N,.* )4*V,+q7 ,U +4G@6 7VH*/K47

!  I)/,-4 /G,74- 7VH*/K @7 !9 ,34) 7VH*/K @7 >

~A 

~B 

Connection when 

A&~B | ~A&B 

A&~B 

~A&B  = A^B 

Page 20: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 20/79

Carnegie Mellon

20

V(*(.); !11;()* F;-(9.)$

!  B5(.)#( 1* !"# ?(0#1.$

!  D34+@0,)7 @33GH4- NH*VH74

!  F;; 17 #I( E.15(.6($ 17 !11;()* F;-(9.) F55;'

01101001

& 01010101

01000001

01101001

| 01010101

01111101

01101001

^ 01010101

00111100

~ 01010101

1010101001000001 01111101 00111100 10101010

Page 21: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 21/79

Carnegie Mellon

21

4(5.($(*6*- Y C)*"5/;)6*- =(#$

!  4(5.($(*#)61*

!  RH-*K V NH* C4/*,+ +43+474)*7 7.N74*7 ,U r>9 s9 VT!t

!  @[ m ! HU [  :

>!!>!>>! r >9 %9 "9 = t!  ! "#$%&'(

!  >!>!>!>! r >9 $9 ̀ 9 = t

!  ! "#$%&'(

!  B5(.)61*$!  j ()*4+74/0,) >!>>>>>! r >9 = t

!  n O)H,) >!!!!!>! r >9 $9 %9 ̀ 9 "9 = t

!  p 56224*+H/ -HW4+4)/4 >>!!!!>> r $9 %9 ̀ 9 " t

!  o 1,23G424)* !>!>!>!> r !9 %9 "9 S t

Page 22: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 22/79

Carnegie Mellon

22

!"#:N(<(; B5(.)61*$ "* G

!  B5(.)61*$ % |% ~% ^ F<)";)9;( "* G

!  :33G6 *, @)6 XH)*4;+@GY -@*@ *634

!  long, int, short, char, unsigned 

!  iH4V @+;.24)*7 @7 NH* C4/*,+7

:+;.24)*7 @33GH4- NH*#VH74!  >R)85;($ ZGI). +)#) #'5([

!  ~0x41 ! 0xBE 

!  ~010000012 ! 101111102 

!  ~0x00 ! 0xFF 

!

  ~000000002 !

 111111112 

!  0x69 & 0x55 ! 0x41 

!  011010012 & 010101012 ! 010000012 

!  0x69 | 0x55 ! 0x7D 

!  011010012 | 010101012 ! 011111012

 

Page 23: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 23/79

Carnegie Mellon

23

G1*#.)$#2 N1-"0 B5(.)61*$ "* G

!  G1*#.)$# #1 N1-"0); B5(.)#1.$

!  &&, ||, ! 

!  iH4V > @7 XM@G74Y

!  :)6*KH); ),)]4+, @7 Xf+.4Y

:GV@67 +4*.+) > ,+ !!  I@+G6 *4+2H)@0,)

>R)85;($ Z0I). +)#) #'5([!  !0x41 !  0x00 

!  !0x00 !  0x01 

!!0x41 !  0x01 

!  0x69 && 0x55 !  0x01 

!  0x69 || 0x55 !  0x01 

!  p && *p _@C,H-7 ).GG 3,H)*4+ @//477a

Page 24: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 24/79

Carnegie Mellon

24

=I"\ B5(.)61*$

!  N(\ =I"\2 x << y 

!  5KHL NH*#C4/*,+ x G4L y 3,7H0,)7

 T fK+,V @V@6 4J*+@ NH*7 ,) G4L

!  MHGG VH*K 0E7 ,) +H;K*

!  4"-I# =I"\2

x >> y

 !  5KHL NH*#C4/*,+ x +H;K* y 3,7H0,)7

!  fK+,V @V@6 4J*+@ NH*7 ,) +H;K*

!  8,;H/@G 7KHL

!  MHGG VH*K 0E7 ,) G4L

:+H*K240/ 7KHL

!  ?43GH/@*4 2,7* 7H;)Hg/@)* NH* ,) +H;K*

!  ]*+(^*(+ !(I)<"1.

!  5KHL @2,.)* u > ,+ v V,+- 7H]4

01100010Argument x

00010000<< 3

00011000Log. >> 2

00011000Arith.

>> 2

10100010Argument x

00010000<< 3

00101000Log. >> 2

11101000Arith. >> 2

0001000000010000

0001100000011000

0001100000011000

00010000

00101000

11101000

00010000

00101000

11101000

Page 25: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 25/79

Carnegie Mellon

25

31+)'2 !"#$% !'#($% )*+ ,*#(-(.$

!  4(5.($(*6*- "*71.8)61* )$ 9"#$

!  !"#:;(<(; 8)*"5/;)61*$

!  ,*#(-(.$

!  4(5.($(*#)61*2 /*$"-*(+ )*+ $"-*(+

1,)C4+7H,)9 /@70);

!  IJ3@)-H);9 *+.)/@0);

!  :--H0,)9 )4;@0,)9 2.G03GH/@0,)9 7KHLH);

!  =/88).'

Page 26: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 26/79

Carnegie Mellon

26

>*01+"*- ,*#(-(.$

short int x = 15213;short int y = -15213;

!  G short _ 9'#($ ;1*-

!  ="-* !"#

M,+ $E7 /,23G424)*9 2,7* 7H;)Hg/@)* NH* H)-H/@*47 7H;)

!  > U,+ ),))4;@0C4

!  ! U,+ )4;@0C4

]*$"-*(+ 3P1 $̀ G185;(8(*#

="-*

!"#

Page 27: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 27/79

Carnegie Mellon

27

>*01+"*- >R)85;( ZG1*#a[x = 15213: 00111011 01101101y = -15213: 11000100 10010011

Page 28: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 28/79

Carnegie Mellon

28

U/8(."0 4)*-($

!  ]*$"-*(+ ?);/($

!  *+,-   m >

>>>s>

!  *+./   m $0  T !

!!!s!

! 3P1`$ G185;(8(*# ?);/($

!  1+,-   m T$0  T!

!>>s>

!  1+./   m $0  T! T !

>!!s!

! B#I(. ?);/($

!  ^H).7 !

!!!s!

?);/($ 71. !  @ Wb

Page 29: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 29/79

Carnegie Mellon

29

?);/($ 71. O"c(.(*# J1.+ ="D($

!  B9$(.<)61*$

!  n1+,- n m 1+./  l !

!  :76224*+H/ +@);4

!  *+./    m $ k 1+./  l !

!  G E.1-.)88"*-

!  wH)/G.-4 uGH2H*7<Kx

!  B4/G@+47 /,)7*@)*79 4<;<9

!  O8D\by^:e

!  8D\by^:e

!  8D\by^(\

[email protected] 3G@z,+2 734/Hg/

Page 30: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 30/79

Carnegie Mellon

30

]*$"-*(+ Y ="-*(+ U/8(."0 ?);/($

!  >d/"<);(*0(!  5@24 4)/,-H);7 U,+ ),))4;@0C4

[email protected]

!  ]*"d/(*($$ 

!  IC4+6 NH* 3@h4+) +43+474)*7

.)H{.4 H)*4;4+ [email protected]!  I@/K +43+474)*@NG4 H)*4;4+ K@7

.)H{.4 NH* 4)/,-H);

!  G)* ,*<(.# C)55"*-$

!  O$A_ / a m A$O#!_ / a

AH* 3@h4+) U,+ .)7H;)4-H)*4;4+

!  f$A_ / a m A$f#!_ / a

!  AH* 3@h4+) U,+ *V,E7 /,23

H)*4;4+

 2 !_3Z # [!_]Z # [>>>> >

>>>! !

>>!> $

>>!! %

>!>> `

>!>! "

>!!> =

>!!! S

 Tcc

 TSQ

 T=!>

 T"!!

 T`!$

 T%!%

 T$!`

 T!!"

!>>>

!>>!

!>!>

!>!!

!!>>

!!>!

!!!>

!!!!

>

!

$

%

`

"

=

S

Page 31: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 31/79

Carnegie Mellon

31

31+)'2 !"#$% !'#($% )*+ ,*#(-(.$

!  4(5.($(*6*- "*71.8)61* )$ 9"#$

!  !"#:;(<(; 8)*"5/;)61*$

!  ,*#(-(.$

!  ?43+474)*@0,)& .)7H;)4- @)- 7H;)4-

G1*<(.$"1*% 0)$6*-

!  IJ3@)-H);9 *+.)/@0);

!  :--H0,)9 )4;@0,)9 2.G03GH/@0,)9 7KHLH);

!  =/88).'

Page 32: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 32/79

Carnegie Mellon

32

f$O

f$A A$O

3P1 $̀ G185;(8(*# ]*$"-*(+

^@H)*@H) 5@24 AH* Z@h4+)

 x 

ux 

 X  

C)55"*- !(#P((* ="-*(+ Y ]*$"-*(+

O$f

O$A A$f

3P1 $̀ G185;(8(*#]*$"-*(+

^@H)*@H) 5@24 AH* Z@h4+)

ux 

 x 

 X  

!  C)55"*-$ 9(#P((* /*$"-*(+ )*+ #P1`$ 0185;(8(*# */89(.$2

e((5 9"# .(5.($(*#)61*$ )*+ .("*#(.5.(#

Page 33: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 33/79

Carnegie Mellon

33

C)55"*- ="-*(+ ]*$"-*(+

0

1

2

3

4

5

6

7

-8

-7

-6

-5

-4

-3

-2

-1

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

]_3

3_]

Page 34: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 34/79

Carnegie Mellon

34

C)55"*- ="-*(+ ]*$"-*(+

0

1

2

3

4

5

6

7

-8

-7

-6

-5

-4

-3

-2

-1

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

@

Hf: Wb

Page 35: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 35/79

Carnegie Mellon

35

+ + + + + +• • •

- + + + + +• • •

ux 

 x 

w –1  0 

4(;)61* 9(#P((* ="-*(+ Y ]*$"-*(+

N).-( *(-)6<( P("-I#

3456748

N).-( 51$"6<( P("-I#

f$O

f$A A$O

3P1 $̀ G185;(8(*# ]*$"-*(+

^@H)*@H) 5@24 AH* Z@h4+)

 x 

ux 

 X  

Page 36: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 36/79

Carnegie Mellon

36

>

1+./

1+,-

 T! T$

>

*+./*+./  T ! 

1+./1+./ l ! 

$E7 1,23G424)*

?@);4

O)7H;)4-?@);4

G1*<(.$"1* ?"$/);"D(+

!  _`$ G185a#

 ]*$"-*(+

!  D+-4+H); ()C4+7H,)

!  \4;@0C4 # AH; Z,7H0C4

Page 37: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 37/79

Carnegie Mellon

37

="-*(+ <$a ]*$"-*(+ "* G

!  G1*$#)*#$

!  A6 [email protected]* @+4 /,)7H-4+4- *, N4 7H;)4- H)*4;4+7

!  O)7H;)4- HU K@C4 XOY @7 7.|J

0U, 4294967259U

G)$6*-!  IJ3GH/H* /@70); N4*V44) 7H;)4- j .)7H;)4- 7@24 @7 O$f @)- f$O

int tx, ty;

unsigned ux, uy;

tx = (int) ux;

uy = (unsigned) ty;

!  (23GH/H* /@70); @G7, ,//.+7 CH@ @77H;)24)*7 @)- 3+,/4-.+4 /@GG7

tx = ux;

uy = ty;

Page 38: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 38/79

Carnegie Mellon

38

0 0U == /*$"-*(+ 

-1 0 < $"-*(+ 

-1 0U > /*$"-*(+ 

2147483647 -2147483648  > $"-*(+

2147483647U -2147483648  < /*$"-*(+ 

-1 -2  > $"-*(+ 

(unsigned) -1 -2  > /*$"-*(+ 

2147483647 2147483648U  < /*$"-*(+ 

2147483647 (int) 2147483648U  > $"-*(+ 

G)$6*- =/.5."$($!

  >R5.($$"1* ><);/)61*

(U *K4+4 H7 @ 2HJ ,U .)7H;)4- @)- 7H;)4- H) 7H);G4 4J3+477H,)9

$%&'() *+,-($ %./,%0%1,2 0+$1 13 -'$%&'()

! ()/G.-H); /,23@+H7,) ,34+@0,)7 <% > % ==% <=% >=

! IJ@23G47 U,+9  m %$& 3C,U @ :_%Wgh%gAi%bgA % 3CFj @ _%Wgh%gAi%bgh

G1*$#)*#W   G1*$#)*#_   4(;)61* ><);/)61*> >O

#! >

#! >O

$!`S`c%=`S #$!`S`c%=`S#!

$!`S`c%=`SO #$!`S`c%=`S#!#! #$

_.)7H;)4-a#! #$

$!`S`c%=`S $!`S`c%=`cO

$!`S`c%=`S _H)*a $!`S`c%=`cO

Page 39: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 39/79

Carnegie Mellon

39

G1+( =(0/."#' >R)85;(

!  ="8";). #1 01+( 71/*+ "* T.((!=O`$ "85;(8(*#)61* 17

-(#5((.*)8(

!  3I(.( ).( ;(-"1*$ 17 $8).# 5(15;( #.'"*- #1 ^*+

</;*(.)9";"6($ "* 5.1-.)8$

/* Kernel memory region holding user-accessible data */#define KSIZE 1024char kbuf[KSIZE];

/* Copy at most maxlen bytes from kernel region to user buffer */int copy_from_kernel(void *user_dest, int maxlen) {

/* Byte count len is minimum of buffer size and maxlen */int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len);return len;

}

Page 40: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 40/79

Carnegie Mellon

40

3'5"0); ]$)-(

/* Kernel memory region holding user-accessible data */#define KSIZE 1024char kbuf[KSIZE];

/* Copy at most maxlen bytes from kernel region to user buffer */int copy_from_kernel(void *user_dest, int maxlen) {

/* Byte count len is minimum of buffer size and maxlen */int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len);return len;

}

#define MSIZE 528

void getstuff() {char mybuf[MSIZE];copy_from_kernel(mybuf, MSIZE); printf(“%s\n”, mybuf);

}

Page 41: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 41/79

Carnegie Mellon

41

C);"0"1/$ ]$)-(

/* Kernel memory region holding user-accessible data */#define KSIZE 1024char kbuf[KSIZE];

/* Copy at most maxlen bytes from kernel region to user buffer */int copy_from_kernel(void *user_dest, int maxlen) {

/* Byte count len is minimum of buffer size and maxlen */int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len);return len;

}

#define MSIZE 528

void getstuff() {char mybuf[MSIZE];copy_from_kernel(mybuf, -MSIZE);. . .

}

/* Declaration of library function memcpy */void *memcpy(void *dest, void *src, size_t n);

Page 42: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 42/79

Carnegie Mellon

42

=/88).'

G)$6*- ="-*(+ k ]*$"-*(+2 !)$"0 4/;($!  !"# 5)S(.* "$ 8)"*#)"*(+

!  !/# .("*#(.5.(#(+

G)* I)<( /*(R5(0#(+ (c(0#$2 )++"*- 1. $/9#.)06*- _P

!  >R5.($$"1* 01*#)"*"*- $"-*(+ )*+ /*$"-*(+ "*#

!  int H7 /@7* *, unsigned}}

Page 43: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 43/79

Carnegie Mellon

43

31+)'2 !"#$% !'#($% )*+ ,*#(-(.$

!  4(5.($(*6*- "*71.8)61* )$ 9"#$

!  !"#:;(<(; 8)*"5/;)61*$

!  ,*#(-(.$

!  ?43+474)*@0,)& .)7H;)4- @)- 7H;)4-

1,)C4+7H,)9 /@70);

!  >R5)*+"*-% #./*0)6*-

!  :--H0,)9 )4;@0,)9 2.G03GH/@0,)9 7KHLH);

!  =/88).'

Page 44: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 44/79

Carnegie Mellon

44

="-* >R#(*$"1*

!  3)$e2

!  bHC4) 0 #NH* 7H;)4- H)*4;4+ /  

!  1,)C4+* H* *, 0 l: #NH* H)*4;4+ VH*K 7@24 [email protected]

!  4/;(2

!  ^@q4 :  /,3H47 ,U 7H;) NH*&

!   2  $ m  / 0  T! 9s9 / 0  T! 9 / 0  T! 9 / 0  T$ 9s9 / >

4  015"($ 17 C=!

• • • X  

 X  $ • • • • • •

• • •

0

0:

Page 45: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 45/79

Carnegie Mellon

45

="-* >R#(*$"1* >R)85;(

!  G1*<(.6*- 7.18 $8);;(. #1 ;).-(. "*#(-(. +)#) #'5(

!  G )/#18)60);;' 5(.71.8$ $"-* (R#(*$"1*

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

Decimal Hex Binary

x 15213 3B 6D 00111011 01101101

ix 15213 00 00 3B 6D 00000000 00000000 00111011 01101101

y -15213 C4 93 11000100 10010011

iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011

Page 46: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 46/79

Carnegie Mellon

46

=/88).'2

>R5)*+"*-% 3./*0)6*-2 !)$"0 4/;($

!  >R5)*+"*- Z(a-a% $I1.# "*# #1 "*#[

!  O)7H;)4-& ]4+,7 @--4-

!  5H;)4-& 7H;) 4J*4)7H,)

A,*K 6H4G- 4J34/*4- +47.G*

3./*0)6*- Z(a-a% /*$"-*(+ #1 /*$"-*(+ $I1.#[

!  O)7H;)4-~7H;)4-& NH*7 @+4 *+.)/@*4-

!  ?47.G* +4H)*4+3+4*4-

!  O)7H;)4-& 2,- ,34+@0,)

!  5H;)4-& 7H2HG@+ *, 2,-

!  M,+ 72@GG ).2N4+7 6H4G-7 4J34/*4- N4K@CH,.+

Page 47: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 47/79

Carnegie Mellon

47

31+)'2 !"#$% !'#($% )*+ ,*#(-(.$

!  4(5.($(*6*- "*71.8)61* )$ 9"#$

!  !"#:;(<(; 8)*"5/;)61*$

!  ,*#(-(.$

!  ?43+474)*@0,)& .)7H;)4- @)- 7H;)4-

1,)C4+7H,)9 /@70);

!  IJ3@)-H);9 *+.)/@0);

!  F++"61*% *(-)61*% 8/;65;"0)61*% $I"\"*-

!  =/88).'

Page 48: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 48/79

Carnegie Mellon

48

U(-)61*2 G185;(8(*# Y ,*0.(8(*#

!  G;)"82 T1;;1P"*- K1;+$ 71. _`$ G185;(8(*#

~x + 1 == -x

!  G185;(8(*#

!  DN74+C@0,)& ~x + x == 1111…111 == -1

!  G185;(#( E.117Q 

! > > ! > !! !J> ! ! > ! >> >oJl

! ! ! ! ! !! !#!

Page 49: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 49/79

Carnegie Mellon

49

G185;(8(*# Y ,*0.(8(*# >R)85;($

R @ Wl_Wi

R @ m

Page 50: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 50/79

Carnegie Mellon

50

]*$"-*(+ F++"61*

!  =#)*+).+ F++"61* T/*061*

!  (;),+47 /@++6 ,.*3.*

!  ,85;(8(*#$ C1+/;). F."#I8(60

8   m O:--0 _; 9 < a m ; l <  2,- $0

• • •

• • •

v + 

• • •u + v 

• • •

f+.4 5.2& 0 l! NH*7

D34+@)-7&0  NH*7

BH7/@+- 1@++6& 0  NH*7 UAddw(u , v)

 

Page 51: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 51/79

Carnegie Mellon

51

?"$/);"D"*- ZC)#I(8)60);[ ,*#(-(. F++"61*

! ,*#(-(. F++"61*

! `#NH* H)*4;4+7 ;9 <  

! 1,23.*4 *+.4 7.2

:--`_; 9 < a

[email protected] H)/+4@74 GH)4@+G6VH*K ; @)- <

! M,+27 3G@)@+ 7.+U@/4

F++gZ- % * [

-

*

Page 52: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 52/79

Carnegie Mellon

52

?"$/);"D"*- ]*$"-*(+ F++"61*

J.)5$ F.1/*+

!  (U *+.4 7.2 v $0  

!  :* 2,7* ,)/4

>

$0

$0 l!

]F++gZ- % * [

-

*

3./( =/8

C1+/;). =/8

DC4+•,V

B<(.n1P

Page 53: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 53/79

Carnegie Mellon

53

C)#I(8)60); E.15(.6($

!  C1+/;). F++"61* T1.8$ )* 56(,%+' 783-/ 

!  G;1$(+ .)-4+ @--H0,)

> & O:--0 _; 9 < a &  $0  T!

!  G188/#)6<(

O:--0 _; 9 < a m O:--0 _<  9 ;a!  F$$10")6<(

O:--0 _= 9 O:--0 _; 9 < aa m O:--0 _O:--0 _= 9 ; a9 < a

!  m H7 @--H0C4 H-4)0*6

O:--0 _; 9 >a m ; 

IC4+6 4G424)* K@7 @--H0C4 "*<(.$(!  84* O1,230 _; a m $0  T ; 

O:--0 _; 9 O1,230 _;  aa m >

Page 54: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 54/79

Carnegie Mellon

54

3P1`$ G185;(8(*# F++"61*

!  3F++ )*+ ]F++ I)<( ,+(*60); !"#:N(<(; !(I)<"1.

!  5H;)4- C7< .)7H;)4- @--H0,) H) 1&

int s, t, u, v;

s = (int) ((unsigned) u + (unsigned) v);t = u + v

!  RHGG ;HC4 s == t

• • •

• • •

+

• • •u + v 

• • •

f+.4 5.2& 0 l! NH*7

D34+@)-7&0  NH*7

BH7/@+- 1@++6& 0  NH*7 TAddw(u , v) 

Page 55: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 55/79

Carnegie Mellon

55

3F++ B<(.n1P

!  T/*061*);"#'

!  f+.4 7.2 +4{.H+47 0 l!

NH*7

!  B+,3 ,W ^5A

f+4@* +42@H)H); NH*7 @7$E7 /,23< H)*4;4+

 T$0 T! T!

 T$0

>

$0 T!

$0  T!

3./( =/8

3F++ 4($/;#

W >>>s>

W >!!s!

m >>>s>

m !>>s>

m !!!s!

!>>s>

>>>s>

>!!s!

Z,7DC4+

\4;DC4+

Page 56: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 56/79

Carnegie Mellon

56

?"$/);"D"*- _`$ G185;(8(*# F++"61*

!  ?);/($

!  `#NH* *V,E7 /,23<

!  ?@);4 U+,2 #c *, lS

!  J.)5$ F.1/*+!  (U 7.2 ' $0  T! 

!  A4/,247 )4;@0C4

!  :* 2,7* ,)/4

!  (U 7.2 u T$0  T! 

A4/,247 3,7H0C4

!  :* 2,7* ,)/4

3F++gZ- % * [

-

*

E1$B<(.

U(-B<(.

Page 57: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 57/79

Carnegie Mellon

57

GI).)0#(."D"*- 3F++

T/*061*);"#'

!  f+.4 7.2 +4{.H+47 0 l! NH*7

!  B+,3 ,W ^5A

!  f+4@* +42@H)H); NH*7 @7 $E7

/,23< H)*4;4+

ZU(-B<(.[

ZE1$B<(.[

u

v

u > x >u >

x >

\4;@0C4 DC4+•,V

Z,7H0C4 DC4+•,V

3F++Z- % * [

2w

2w

Page 58: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 58/79

Carnegie Mellon

58

C)#I(8)60); E.15(.6($ 17 3F++

!  ,$181.5I"0 V.1/5 #1 /*$"-*(+$ P"#I ]F++

!  f:--0 _; 9 < a m O$f_O:--0 _f$O_; a9 f$O_< aaa

!  5H)/4 N,*K K@C4 H-4)0/@G NH* 3@h4+)7

!  3P1`$ G185;(8(*# ]*+(. 3F++ T1.8$ ) V.1/5

!  1G,74-9 1,22.*@0C49 :77,/H@0C49 > H7 @--H0C4 H-4)0*6

!  IC4+6 4G424)* K@7 @--H0C4 H)C4+74

Page 59: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 59/79

Carnegie Mellon

59

C/;65;"0)61*

!  G185/6*- >R)0# E.1+/0# 17 0 :9"# */89(.$ / % >

!  IH*K4+ 7H;)4- ,+ .)7H;)4-

!  4)*-($ 

!  O)7H;)4-& > € /  k >  € _$0  T !a $  m $$0  T $0 l! l !

O3 *, $0  NH*7!  fV,E7 /,23G424)* 2H)& /  k >   v _T$0  T!ak_$0  T! T!a m T$$0  T$ l $0  T!

!  O3 *, $0  T! NH*7

!  fV,E7 /,23G424)* 2@J& /  k >  € _T$0  T!a $  m $$0  T$

!  O3 *, $0  NH*79 N.* ,)G6 U,+ _1+,-0 a$

!  C)"*#)"*"*- >R)0# 4($/;#$!  R,.G- )44- *, q443 4J3@)-H); V,+- 7H]4 VH*K 4@/K 3+,-./* /,23.*4-

!  B,)4 H) 7,LV@+4 N6 X@+NH*+@+6 3+4/H7H,)Y @+H*K240/ 3@/q@;47

C i

Page 60: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 60/79

Carnegie Mellon

60

]*$"-*(+ C/;65;"0)61* "* G

!  =#)*+).+ C/;65;"0)61* T/*061*

!  (;),+47 KH;K ,+-4+ 0  NH*7

!  ,85;(8(*#$ C1+/;). F."#I8(60

O^.G*0 _; 9 < a m ;  <  2,- $0

• • •

• • •

*

• • •u · v 

• • •

f+.4 Z+,-./*& $k0   NH*7

D34+@)-7&0  NH*7

BH7/@+-0  NH*7& 0  NH*7

UMultw(u , v) 

• • •

C i M ll

Page 61: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 61/79

Carnegie Mellon

61

G1+( =(0/."#' >R)85;( o_

!  =]U jO4 ;"9.).'!  RH-4G6 .74- GHN+@+6 U,+ *+@)7U4++H); -@*@ N4*V44) 2@/KH)47

void* copy_elements(void *ele_src[], int ele_cnt, size_t ele_size);

ele_src

8);;10Z(;(p0*# q (;(p$"D([

C i M ll

Page 62: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 62/79

Carnegie Mellon

62

jO4 G1+(

void* copy_elements(void *ele_src[], int ele_cnt, size_t ele_size) {/** Allocate buffer for ele_cnt objects, each of ele_size bytes* and copy from locations designated by ele_src*/void *result = malloc(ele_cnt * ele_size);if (result == NULL)

/* malloc failed */return NULL;

void *next = result;int i;for (i = 0; i < ele_cnt; i++) {

/* Copy object i to destination */ memcpy(next, ele_src[i], ele_size);

/* Move pointer to next memory region */next += ele_size;

}return result;

}

C i M ll

Page 63: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 63/79

Carnegie Mellon

63

jO4 ?/;*(.)9";"#'

!  JI)# "72

!  ele_cnt m $$> l !

! ele_size  m `>Q= m $

!$

!  :GG,/@0,) m ‚‚

!  K1P 0)* , 8)e( #I"$ 7/*061* $(0/.(Q

8);;10Z(;(p0*# q (;(p$"D([

Carnegie Mellon

Page 64: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 64/79

Carnegie Mellon

64

="-*(+ C/;65;"0)61* "* G

!  =#)*+).+ C/;65;"0)61* T/*061*

!  (;),+47 KH;K ,+-4+ 0  NH*7

!  5,24 ,U VKH/K @+4 -HW4+4)* U,+ 7H;)4-

C7< .)7H;)4- 2.G03GH/@0,)!  8,V4+ NH*7 @+4 *K4 7@24

• • •

• • •

*

• • •u · v 

• • •

f+.4 Z+,-./*& $k0   NH*7

D34+@)-7&0  NH*7

BH7/@+-0  NH*7& 0  NH*7

TMultw(u , v)

 

• • •

Carnegie Mellon

Page 65: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 65/79

Carnegie Mellon

65

E1P(.:17:_ C/;65;' P"#I =I"\

!  B5(.)61*

!  u << k ;HC47 u * 94

!  A,*K 7H;)4- @)- .)7H;)4-

!  >R)85;($

!

 

u << 3 == u * 8!  u << 5 - u << 3 == u * 24

!  ^,7* 2@/KH)47 7KHL @)- @-- U@7*4+ *K@) 2.G03G6

!  1,23HG4+ ;4)4+@*47 *KH7 /,-4 @.*,2@0/@GG6

• • •

0 0 1 0 0 0•••

2k 

 

*u · 2k 

 f+.4 Z+,-./*&0 l:   NH*7

D34+@)-7&0  NH*7

BH7/@+- : NH*7& 0  NH*7 UMultw(u , 2k )

 

•••

k  

• • • 0 0 0•••

TMultw(u , 2k )

 

0 0 0••••••

Carnegie Mellon

Page 66: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 66/79

Carnegie Mellon

66

leal (%eax,%eax,2), %eaxsall $2, %eax

G185";(+ C/;65;"0)61* G1+(

!  G 0185";(. )/#18)60);;' -(*(.)#($ $I"\f)++ 01+( PI(*

8/;65;'"*- 9' 01*$#)*#

int mul12(int x){return x*12;

}

t <- x+x*2return t << 2;

G T/*061*

G185";(+ F."#I8(60 B5(.)61*$ >R5;)*)61*

Carnegie Mellon

Page 67: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 67/79

Carnegie Mellon

67

]*$"-*(+ E1P(.:17:_ O"<"+( P"#I =I"\

!  r/16(*# 17 ]*$"-*(+ 9' E1P(. 17 _!  u >> k ;HC47

(

 u / 94)

 

!  O747 G,;H/@G 7KHL

0 0 1 0 0 0•••

2k  

 /

u / 2k  BHCH7H,)&

D34+@)-7& •••

k  

••• •••

•••0 0 0••• •••

( u / 2k )  •••?47.G*&

<

AH)@+6 Z,H)*

0

0 0 0•••0

Carnegie Mellon

Page 68: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 68/79

Carnegie Mellon

68

shrl $3, %eax

G185";(+ ]*$"-*(+ O"<"$"1* G1+(

!  ]$($ ;1-"0); $I"\ 71. /*$"-*(+

!  T1. s)<) ]$(.$

!  8,;H/@G 7KHL V+Hh4) @7 >>>

unsigned udiv8(unsigned x){return x/8;

}

# Logical shiftreturn x >> 3;

G T/*061*

G185";(+ F."#I8(60 B5(.)61*$ >R5;)*)61*

Carnegie Mellon

Page 69: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 69/79

Carnegie Mellon

69

="-*(+ E1P(.:17:_ O"<"+( P"#I =I"\

!  r/16(*# 17 ="-*(+ 9' E1P(. 17 _!  x >> k ;HC47

(

 x / 94)

 

!  O747 @+H*K240/ 7KHL

!  ?,.)-7 V+,); -H+4/0,) VK4) u < 0

0 0 1 0 0 0••• x

 

2k  

 /

 x / 2k  

BHCH7H,)&

D34+@)-7&•••

k  

••• •••

•••0 ••• •••

RoundDown( x / 2k )  •••?47.G*&

<

AH)@+6 Z,H)*

0 •••

Carnegie Mellon

Page 70: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 70/79

Carnegie Mellon

70

G1..(0# E1P(.:17:_ O"<"+(

!  r/16(*# 17 U(-)6<( U/89(. 9' E1P(. 17 _!  R@)*

*

 x / _4+

  _?,.)- f,V@+- >a

!  1,23.*4 @7( (x+_4 -1)/ _4

!  () 1& (x + (1<<k)-1) >> k 

!  AH@747 -HCH-4)- *,V@+- >

G)$( W2 U1 .1/*+"*-

BHCH7,+&

BHCH-4)-&

0 0 1 0 0 0•••

2k  

 /

* u / 2k + 

•••

k  

1 ••• 0 0 0•••

1 •••0 1 1••• <

AH)@+6 Z,H)*

1

0 0 0 1 1 1•••+2k –1 

•••

1 1 1•••

1 ••• 1 1 1•••

:%+$%'& ;+$ '3 (<(01

Carnegie Mellon

Page 71: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 71/79

g

71

G1..(0# E1P(.:17:_ O"<"+( ZG1*#a[

BHCH7,+&

BHCH-4)-&

G)$( _2 41/*+"*-

0 0 1 0 0 0•••

 x 

2k  

 /

*  x / 2k + 

•••

k  

1 ••• •••

1 •••0 1 1••• <

AH)@+6 Z,H)*

1

0 0 0 1 1 1•••+2k –1 

•••

1 ••• •••

:%+$%'& +))$ = 13 >'+, 8($-,1

•••

()/+424)*4- N6 !

()/+424)*4- N6 !

Carnegie Mellon

Page 72: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 72/79

g

72

testl %eax, %eaxjs L4

L3:sarl $3, %eaxret

L4:addl $7, %eaxjmp L3

G185";(+ ="-*(+ O"<"$"1* G1+(

!  ]$($ )."#I8(60 $I"\ 71. "*#

!  T1. s)<) ]$(.$

!  :+H*K< 7KHL V+Hh4) @7 >>

int idiv8(int x){return x/8;

}

if x < 0x += 7;

# Arithmetic shiftreturn x >> 3;

G T/*061*

G185";(+ F."#I8(60 B5(.)61*$ >R5;)*)61*

Carnegie Mellon

Page 73: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 73/79

g

73

F."#I8(602 !)$"0 4/;($

!  F++"61*2

!  O)7H;)4-~7H;)4-& \,+2@G @--H0,) U,GG,V4- N6 *+.)/@*49

7@24 ,34+@0,) ,) NH* G4C4G

!  O)7H;)4-& @--H0,) 2,- $V

!  ^@*K42@0/@G @--H0,) l 3,77HNG4 7.N*+@/0,) ,U $V

5H;)4-& 2,-Hg4- @--H0,) 2,- $V _+47.G* H) 3+,34+ +@);4a 

!  ^@*K42@0/@G @--H0,) l 3,77HNG4 @--H0,) ,+ 7.N*+@/0,) ,U $V

!  C/;65;"0)61*2

!

 

O)7H;)4-~7H;)4-& \,+2@G 2.G03GH/@0,) U,GG,V4- N6 *+.)/@*497@24 ,34+@0,) ,) NH* G4C4G

!  O)7H;)4-& 2.G03GH/@0,) 2,- $V

!  5H;)4-& 2,-Hg4- 2.G03GH/@0,) 2,- $V _+47.G* H) 3+,34+ +@);4a 

Carnegie Mellon

Page 74: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 74/79

74

F."#I8(602 !)$"0 4/;($

!  ]*$"-*(+ "*#$% _`$ 0185;(8(*# "*#$ ).( "$181.5I"0 ."*-$2

"$181.5I"$8 @ 0)$6*-

!  N(\ $I"\

O)7H;)4-~7H;)4-& 2.G03GH/@0,) N6 $q

!  :GV@67 G,;H/@G 7KHL

!  4"-I# $I"\

!  O)7H;)4-& G,;H/@G 7KHL9 -HC _-HCH7H,) l +,.)- *, ]4+,a N6 $q

5H;)4-& @+H*K240/ 7KHL

!  Z,7H0C4 ).2N4+7& -HC _-HCH7H,) l +,.)- *, ]4+,a N6 $q

!  \4;@0C4 ).2N4+7& -HC _-HCH7H,) l +,.)- @V@6 U+,2 ]4+,a N6 $q

O74 NH@7H); *, gJ

Carnegie Mellon

Page 75: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 75/79

75

31+)'2 ,*#(-(.$

!  4(5.($(*#)61*2 /*$"-*(+ )*+ $"-*(+

!  G1*<(.$"1*% 0)$6*-

!  >R5)*+"*-% #./*0)6*-

!  F++"61*% *(-)61*% 8/;65;"0)61*% $I"\"*-

!  =/88).'

Carnegie Mellon

Page 76: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 76/79

76

E.15(.6($ 17 ]*$"-*(+ F."#I8(60

!  ]*$"-*(+ C/;65;"0)61* P"#I F++"61* T1.8$

G188/#)6<( 4"*-

!  :--H0,) H7 /,22.*@0C4 ;+,.3

!  1G,74- .)-4+ 2.G03GH/@0,)

>&

 O^.G*0 

_; 9 < a&

  $0  T!

!  ^.G03GH/@0,) 1,22.*@0C4

O^.G*0 _; 9 < a m O^.G*0 _<  9 ;a

!  ^.G03GH/@0,) H7 :77,/H@0C4

O^.G*0 _= 9 O^.G*0 _; 9 < aa m O^.G*0 _O^.G*0 _= 9 ; a9 < a

! ! H7 2.G03GH/@0C4 H-4)0*6

O^.G*0 _; 9 !a m ; 

!  ^.G03GH/@0,) -H7*+HN.*47 ,C4+ @--0,)

O^.G*0 _= 9 O:--0 _; 9 < aa m O:--0 _O^.G*0 _= 9 ; a9 O^.G*0 _= 9 < aa

Carnegie Mellon

Page 77: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 77/79

77

E.15(.6($ 17 3P1`$ G185a F."#I8(60! 

,$181.5I"0 F;-(9.)$!  O)7H;)4- 2.G03GH/@0,) @)- @--H0,)

!  f+.)/@0); *, 0  NH*7

!  fV,E7 /,23G424)* 2.G03GH/@0,) @)- @--H0,)

!  f+.)/@0); *, 0  NH*7

!  !1#I T1.8 4"*-$!  (7,2,+3KH/ *, +H); ,U H)*4;4+7 2,- $0  

G185)."$1* #1 ZC)#I(8)60);[ ,*#(-(. F."#I8(60

!  A,*K @+4 +H);7

!  ()*4;4+7 ,N46 ,+-4+H); 3+,34+0479 4<;<9

; x > !   ; l <  x <  

; x >9 <  x > !   ;  <  x >

!  fK474 3+,34+047 @+4 ),* ,N464- N6 *V,E7 /,23< @+H*K240/

1+./  + 1 == 1+,- 

15213 * 30426 == -10030 _!=#NH* V,+-7a

Carnegie Mellon

Page 78: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 78/79

78

JI' =I1/;+ , ]$( ]*$"-*(+Q

?3'@1  ]$( s/$# !(0)/$( U/89(. U1**(-)6<(

!  I@76 *, 2@q4 2H7*@q47

unsigned i;

for (i = cnt-2; i >= 0; i--)

a[i] += a[i+1];

1@) N4 C4+6 7.N*G4

#define DELTA sizeof(int)

int i;

for (i = CNT; i-DELTA >= 0; i-= DELTA)

. . .

!  ?3 ]$( JI(* E(.71.8"*- C1+/;). F."#I8(60

!  ^.G03+4/H7H,) @+H*K240/

!  ?3 ]$( JI(* ]$"*- !"#$ #1 4(5.($(*# =(#$

!  8,;H/@G +H;K* 7KHL9 ), 7H;) 4J*4)7H,)

Carnegie Mellon

Page 79: 02 Bits Ints

7/18/2019 02 Bits Ints

http://slidepdf.com/reader/full/02-bits-ints 79/79

,*#(-(. G E/DD;($

•  x < 0 ((x*2) < 0)

•  ux >= 0

• 

x & 7 == 7 (x<<30) < 0

•  ux > -1

• 

x > y -x < -y•  x * x >= 0

•  x > 0 && y > 0 x + y > 0

• 

x >= 0 -x <= 0

•  x <= 0 -x >= 0

• 

(x|-x)>>31 == -1•  ux >> 3 == ux/8

•  x >> 3 == x/8

• 

x & (x-1) != 0 

"*# R @ 711Z[t

"*# ' @ 9).Z[t

/*$"-*(+ /R @ Rt

/*$"-*(+ /' @ 't

,*"6);"D)61*


Recommended