Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
211 views

A Tutorial On Data Representation - Integers, Floating-Point Numbers, and Characters

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters yet another insignificant programming notes.

Uploaded by

Goce Stavreski
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
211 views

A Tutorial On Data Representation - Integers, Floating-Point Numbers, and Characters

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters yet another insignificant programming notes.

Uploaded by

Goce Stavreski
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

yet another insignificant programming notes... | HOME

TABLE OF CONTENTS (HIDE)

A Tutorial on Representation

Data

1 N mb r ytms . u e Ss e

Integers, Floating-point Numbers, and Characters


1. Number Systems

1 D c l ae 0 N mb r ytm . ei ( s 1 ) u e Ss 1 ma B e 1 B ay B s 2 N mb r ytm . i r (ae ) u e Ss 2 n e 1 H xd c l ae 6 N mb r ytm . ea ei ( s 1 ) u e Ss 3 ma B e 1 C n es nf m H xd c loB ay . o vro r 4 i o ea ei t i r ma n 1 C n es nf m B ay oH xd c l . o vro r 5 i o i r t ea ei n ma 1 C n es nf m B s r t D c l ae 0 . o vro r 6 i o ae o ei ( s 1 ) ma B 1 C n es nf m D c l ae 0 t B s . o vro r 7 i o ei ( s 1 )o ae ma B 1 Eec e (u e Ss ms o vro ) . xrssN mb r yt C n es n 8 i e i

2 C mp tr moy D t R pee tt n . o ue Me r & aa e rsnai o 3 I e e R pee tt n .n g r e rsnai t o

H ma b i sue decimal (ae 1 )a d duodecimal (ae 1 )n mb r u n eg s n bs 0 n bs 2 u e ss mso cu t ga dmesrme t (rb b b cue e ae 0f g r yt fr o ni n e n aue nspo a l ea s w h v 1 i es y n a dt ob te) o ues s binary (ae ) u e ss m, s h yae n w i o s C mp tr ue g . b s 2 n mb r yt a te r e ma ef m b ayd i lo o e t ( o na t nios o eai i t o d r o i r it cmp n ns k w s r s tr p rt g n w n ga n a s ) n s ts o a dof ncmp t g w a ouehexadecimal (ae1 ) r t e - n n f I o ui , e l s a . n s b s 6 o octal 4 F ai -on N mb r e rsnain . l t gP i u e R pee tt o n t o (ae ) u e ss msa a b s 8 n mb r yt ,s compact fr fre rsn b ay u es e om o rpee t i r n mb r n . 4 I E7 43 -iS g -rc i F ai -o . E -5 2b i l Peio l t gP 1 E tne sn o n 4 Eec e (o t gp i N mb r . xrssF ai -o t u es 2 i l n n ) 1.1 Decimal (Base 10) Number System 4 I E7 46 -iD u l Peio F ai . E -5 4b o b -rc i l t gP 3 E t e sn o n 4 Moe nF ai -o t e rsnai . 4 r o l t gP i R pee tt n o n n o D c l u e ss m h s e smb l 0 1 2 3 4 5 6 7 8 a d9 cld ei n mb r yt ma e a tn y o : , , , , , , , , , n ,ae s l

3 n-iU s n dItg r . b ni e ne es 1 t g 3 S n dItg r . i e ne es 2 g 3 n-iS nItg r i S nMa n u e e r . b i ne esn i - g i d R pe 3 tg g t 3 n-iS nItg r i 1 C mp me t e r . b i ne esn ' o l n R pe 4 tg s e 3 n-iS nItg r i 2 C mp me t e r . b i ne esn ' o l n R pe 5 tg s e 3 C mp tr ue 'C mp me t e rsna . o ues s 2 o l n R pee t 6 s e 3 R n e f b 2 C mp me t i e Itg . a g o n-i ' o l n S n d ne 7 t s e g 3 D cd g2 C mp me t u es . eo i ' o l n N mb r 8 n s e 3 Eec e Itg r e rsnai ) . xrs ( e e R pee tt n 9 i n o

. h rc E c dn e g digits I ue positional notation. h ti tel s s n i n d i (g t 5 C aatr no i .t ss T a s h e t i i a t it r h, a -g f c g i 5 7b A CI o e aa SA CI S / C6 6 . -i S IC d ( U - S I I I 4 1 t k ,O E mot itio teod r f 0 0(n s r n s tescn r h- s d i s d i s fh re o 1 ^ u i o o e) h eo d i t g) t , g mot it g 5 8b Lt - ( a S / C8 5 -) . -i ai 1 a I I 8 91 2 t n k O E io teod r f 0 1( n)te h dr h- s d iio te re o 1 ^ s fh re o 1 ^ t s h ti i t e , r g mot its fh od r f 0 2 g 5 O h r -iEtni o U - S I(S IEt . te 8b x s n f SA CIA CI x 3 t e o (u de s a ds o .o ea l h n rd) n o n F r xmp , , e 5 U i d ( a S / C1 6 6U i r l h r . n o e a I I 0 4 n es C aa 4 c k O E v a 75=71^ +31^ +51^ 3 02 01 00 5 U F8(n o e rnfr t nF r t 8 . T- U i d Ta s mai oma - 5 c o o 5 U F1 (n o e rnfr t nF r t 1 . T-6 U i d Ta s mai oma 6 c o o We h l e oe d c l u e wt a o t n luf Di mb u y re. sa d n t a ei n mb r i n pi asf l ma h o i f i i ai s x a gt s 5 U F3 (n o e rnfr t nF r t 3 . T-2 U i d Ta s mai oma 7 c o o 5 F r t o Mu i ye e . n o e Tx F . omas f l B t (g U i d ) et i 8 t ., c l 1.2 Binary (Base 2) Number System 5 F r t o Tx Fe . omas f et is 9 l 5 0 Wi o sC . 1 n w ' MDC d p g d o eae B ay n mb rss m h st o smb l 0 a d 1 cld bits. ti a o a i r u e yt n e a w y o : n , ae s l I s l s 5 1 C i s C aatr es . 1 h ee h rc St n e positional notation, r xmp , f ea l o e 5 2 C lt gSq e csfr a k gC aatr . 1 oai e u ne ( R n i h rc ln o n e 110 =124+023+122+121+020 01B ^ ^ ^ ^ ^ 5 3 F raa rga . o Jv Po rmmes j v . i . h r 1 r- a a n o C a s
We sa d n t a b ay n mb r wt a sf B S me po rmmi h l e oe l ir u e i n h uf . o i x rga n g l g a e d n t b ayn mb r wt pex b(g 0 1 0 0 0, r rf b a u g s e oe i r u es i rf 0 e . b 0 1 0 )o pex n n h i ., i wt te i q oe (g b 1 0 1 1 '. i h b s u td e . ' 0 0 1 1 ) h t ., Ab ay its ae a i r d iicld bit.i t i iclda n g l E h b ss ae byte (h 8b u i Po a l g t l w y -i n ? rb b t t y 5 5 Dsl i H x a e & H x d os . 1 i a n e Vl s pyg u e Ei r t 6 Eec e (aa e rsnai ) . xrssD t R pee tt n 1 i o
1/25

5 4 F raa rga . o Jv Po rmmes c a a dS r n 1 r- h r n t i g

6 S mmay Wh B te a o t aa e rsn .u r - y oh r b u D t R pee

www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

b cue = 3. ea s 8 2 )

1.3 Hexadecimal (Base 16) Number System


H xd c l u e ss m ue 1 smb l 0 1 2 3 4 5 6 7 8 9 A B C D E a dF cldhex digits.tsa ea ei n mb r yt ma e ss 6 y o : , , , , , , , , , , , , , , , n , ae s l Ii positional notation, r xmp , f ea l o e
AE =162+31^ +160 3H 01^ 61 41^

We h l e oe h xd c l u e ( soth xwt a uf H S mepo rmmi l g a e d n t h x u es sa d n t a ea ei n mb ri h r e) i sf .o l ma n , h i x rga n a u g s e oe e n mb r g n wt pex x(g 0 1 3 5 ) r rf xwt h x it u td(g x C A D 8 '. i rf 0 e . x A C F, pex i e d iq oe e . ' 3 4 9 B ) h i ., o i h g ., Ec h xd c l its l cldahex digit. s po rmmi l g a e acp l ecs ' 't ' 'a w la ah ea ei d iia o ae ma g s l Mot rga n a u g s ce to r e a o f s e s g n w a l u p r s ' 't ' ' p ec e A o F . a C mp tr ue b ayss m i te i en l p rt n, ste aeb i f m b ayd i l l t n cmp n ns o ues ss i r yt n h i n ra o eai sa h y r utr n e r t o l o i r it e c o i o o e t n ga e r c . H w vrw i go ra i al gsq e c o b ay i icmb r me n err rn . ea ei lytm iue o ee, ri r ed g o e u ne f i r b ss u es t n n n n t o a d r - o e H xd c op ma ss e s sd aa s compact fr o shorthand fr i r b sEc h x its q i l t o4b ayb si. h r a dfr b sa om r o b ay i. ah e d iie u a n t n t g ve i r i,e sot n o 4 i, s n t ., h t flw : oo s l 0 (00) H 00B (D 0) 4 (10) H 00B (D 4) 8 (00) H 10B (D 8) C (10) H 10B (2) 1D 1 (01) H 00B (D 1) 5 (11) H 00B (D 5) 9 (01) H 10B (D 9) D (11) H 10B (3) 1D 2 (00) H 01B (D 2) 6 (10) H 01B (D 6) A (00) H 11B (0) 1D E (10) H 11B (4) 1D 3 (01) H 01B (D 3) 7 (11) H 01B (D 7) B (01) H 11B (1) 1D F (11) H 11B (5) 1D

1.4 Conversion from Hexadecimal to Binary


R p c ec h x it y h 4e u a n b sfr xmp s e l e ah e d ib te q i l t i, ea l , a g ve t o e
ACH=11 01 10 00B 35 00 01 10 11 12H=00 00 01 11B 0A 01 00 00 00

1.5 Conversion from Binary to Hexadecimal


Sat gf m ter h- s b (at i i a t i,e l eec go po 4b s ytee u a n h x it p dte trn r i o h it g mot i l s s n i n b )rp c ah ru f i b h q i l t e d i(a h t e -g f c t a t ve g lt s b s i zr i eesr) o ea l , e - t i wt eo f csay fr xmp s f mo t h n , e
10011B=01 00 11B=2A 01000 00 10 00 4H 1011011B=01 01 10 11B=2CH 0001001 00 00 10 01 2B

Iii otn t n t ta h xd c l u e po i s compact form o shorthand fre rsni b ay i. tsmp r to oe h t ea ei n mb r rv e a a ma d r o rpee t g i r b s n n t

1.6 Conversion from Base r to Decimal (Base 10)


Gvna d ib s r n mb rd - d - d - . . d d d d (ae ) h d c lq i l ts i nb : i e n-it ae u e: n 1 n 2 n 3 . 3 2 1 0 b s r te ei e u a n ig e y g , ma ve v
d- r(-)+d- r(-)+..+d r1+d r0 n1 ^n1 n2 ^n2 . 1 ^ 0 ^

1.7 Conversion from Decimal (Base 10) to Base r


U e e etdd io / ma d rF r xmp , s rp ae is nr i e.o ea l vi e n e
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 2/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

T cnet21 t hxdcml o ovr 6D o eaeia: 211 qoin=6rmidr5 6/6 utet1 eane= 1/6 qoin= rmidr0 61 utet1 eane= 11 /6 qoin= rmidr1(utet0so) utet0 eane= qoin= tp Hne 21 =15 ec, 6D 0H

1.8 Exercises (Number Systems Conversion)


1 C n eth flwn decimal n mb r i obinary a dhexadecimal n mb r . o vrte oo i l g u esn t n u es : a 18 . 0 b 44 . 88 c 90 . 00 2 C n eth flwn b ay u esnoh xd c ln d c l u es . o vrte oo i i r n mb r i l g n t ea ei a d ei n mb r ma ma : a 10010 . 00100 b 1000 . 0000 c 111111 . 000000 3 C n eth flwn h xd c l u esnob ay n d c l u es . o vrte oo i ea ei n mb r i l g ma t i r a d ei n mb r n ma : a ACE . BD b 13 . 24 c 8F . 0 4 [ D ] r . T O moe O

Answers: Y ucudueteWi o sC l l o (a c e e t cr o t u e ss m cn es n b stn it o ol s h n w ' a u trc l . x )o ar u n mb r yt o vro , y et g t o d ca y e i i


te c ni mo e(u " l Sl tVe " n C o s "rga h si ti d . n c c e f c R a " e c "i me u e w h oe Po rmme" rSi ti mo e ro "c ni " d . e f c ) 1 1 0 1 0 ,0 1 1 1 0 0 B1 0 1 0 1 1 0 B6 H1 F H2 2 H . 1 1 0 B1 0 0 1 1 0 0 ,0 0 1 0 0 0 0 ,C ,2 0 ,3 8 . 2 2 8 ,0 ,A H5 6 ,2 D2 3 D . 1 H8 HA A ,3 D1 8 ,7 0 . 3 1 1 1 1 1 0 1 0 1 1 B1 0 0 0 1 1 0 ,0 0 0 0 1 1 ,0 7 0 ,6 0 ,0 3 . . 0 0 0 1 1 0 1 1 1 0 ,0 1 0 1 0 0 B1 0 0 0 1 1 B7 3 1 D4 6 D2 6 D

2. Computer Memory & Data Representation


C mp tr ss fixed number of bits t rpee t p c o d t, h hcu b a u e, caatro oh r Ano ue ue a o e rsn a i e f aaw i o l e n mb ra h rc , r tes e c d e . b s rg l ai cnrpee t pt 2 nd t c e ti .o ea l a -ime r l ai cnh l o eo tee i t a e o t n a e rsn u o ^ ii t ni sF r xmp , 3b t o c o sn t e e t moyo t n a o n fh s c o d e h b ay atrs0 0 0 1 0 0 0 1 1 0 1 1 1 0 o 1 1 H ne icnrpee t t s 8d t c e ti . o i t i r p t n: 0 , 0 , 1 , 1 , 0 , 0 , 1 , r 1 . e c, a e rsn a mot ii t ni sY u g n e t sn t e cu ue h m t rpee t u es t 7 n mb r 8 8 t 8 8 ,h rc r ''o'' r pt 8k d o f i le o l s te o e rsn n mb r 0 o , u es 8 1 o 8 8 caat sA t H, u o i s fr t i d e o n us k a p , rn eb n n ; r pt 8k d o a i llei ,i rec p l oa g , a a ao u o i s f n si ln t e,t e n ma k o g . Itg r frea l cn b rpee td i 8b , 6b ,3 -i o 6 -i Y u a te po rmme, h oe a ne es o xmp , a e e rsne n -i 1 -i 2b r 4b . o , s h rga , e t t t t r co s n a po r t b - n t fry u i e es Y u co e wli oecnt i o tern e o i e esta cn b p rpi e il gh o o r n g r o r h i i mp s o s a t n h a g f n g r h t a e a te t . c l rn t rpee td B s e teb - n t, ni e e cnb rpee tdi vr u representation sh me, ., ni e v. e rsne . ei s h il gh a n g r a e e rsne n ai s d te t o ce se . s n d s gu g s n di e esA 8b u s n di e e h s rn e f t 2 5 w i a 8b s n di e e h s rn eo -2 t i e n g r n -i ni e n g r a a a g o 0 o 5 , he n -i i e n g r a a a g f1 8 o g t . t g t l tg t 1 7- ohrpee t g2 6d t c n mb r 2 b t e rsni 5 ii t u es n sn . Iii otn t n t ta acmp tr moyl ai mee stores a binary pattern.ts ni l u t y u a te tsmp r t o oe h t o ue me r o t n rl a c o y Iie te p o o , s h ry po rmme, o d c eo h w teep t rsaet b interpreted. o ea l te 8b b ayp t r " 1 0 rga r t ei n o h s at n r o e d e F r xmp , h -i i r at n 0 0 e t n e 0 0 B cnb i epee a a u s n di e e 6 , r nA CIh rc r A , r o sc tnomai k o no l 0 1 " a en rrtd s n ni e n g r 5 o a S Icaat ' ' o sme er i r t n n w n t g t e e f o y t y u I oh r od, o h v t fs d c eh w t rpee t p c o d t i ab ayp t r b fr teb ay o o .n te w rsy u ae o it ei o o e rsn a i e f aa n i r at n e e h i r r d e n e o n p t rs k sne T ei epeai o b ayp t r iclddata representation o encoding. utemoe ii at n mae e s. h n rrtt n f i r at n s ae e t o n e l r F r r r,ts h i otn ta ted t rpee tt nsh me aea re -p nb a tep re,e i uta s n ad n e t b mp r t h t h aa e rsnai ce s r gedu o y l h at si.n s i t d rs ed o e a o l i ., d r l a
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 3/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

fr l e a ds a hlflw d omu td n t i t oo e . a rg y l O c y ud c e o ted t rpee tt nsh me cr i cnt i s i p rcl ,h peio a drn ewl e ne o ei d n h aa e rsnai ce , et n o s a t n atu rte rc i n a g ib d o a rn , i a sn l i oe . e c, ii otn t u d r a ddata representation t w i correct a dhigh-performance po rms mp sd H nei smp r to n es n t a t o re t n rga .

Rosette Stone and the Decipherment of Egyptian Hieroglyphs


E y t n h rg p s(et olt w r ue g pi i o l h n x t- f ee sd a e y - e) b te a c n E y t n s c 4 0 B . y h ni t g pi s i e 0 0 C e a n U fr n tl s c 5 0 D n o e cu not ae , i e 0 A , o n o l u y n d l g rra tea c n E y t n h rg p s o e ed h ni t g pi i o l h, n e a e y u t ter-i o eyo teR st So ei ni h ed cvr f h oet tn n l s e 1 9 b N p l n t o (ui N p l n 7 9 y ao o ' r p d r g ao o ' e s o n e s E y t n i ai )n a te tw o R si g pi n s n er h o n f ah a v o d (oet) te i D l . R st i h Nl e a an e t T e R st So e (f i i cb d wt a h oet tn lt s n r e i a e) si h d c ei 1 6 C o b h lo K gPo myV er n 9 B n e a f i tl e f n e . T ed c ea p asi three sr t teu p r h er p er n e cps h p e i : tx i Ancient Egyptian hieroglyphs, te et s h mi l p ro D moisr ta dtel et d e ot n e t cp, n h o s Ancient Greek. ea s ipee t d i c i w B cue t rsns esnil tesmetx i a tresr t a d A c n G ekcu slb u d r o d i po i dtekyt te se t l h a ay etn l he cps n ni t re o l t e n es o ,t rv e h e o h l i , e d i l t d d c h r n o te g pi h rg p s ei eme t fh E y t n i o l h. p a e y T e rl fh s r iu l s o k o te no i sh metees ow y h t o cnd cd te aa h moao te t ys n s y u n w h e cd g ce ,h r in a ta y u a eo e h d t. o e n R f e c a di g sWip d . e rne n ma e: k e i e i a

3. Integer Representation
Itg r aewhole numbers o fixed-point numbers wt terd p i fixed a e tel s s n i n b . h yae ne es r r i h ai o t h x n f r h e t i i at i Te r t a -g f c t cnrs t real numbers o floating-point numbers, h r tep si o terd p i vr sIii otn t tk o t to a r w ee h oi n fh a i o t ai .tsmp r t o ae t o x n e a n t ta i e esa df ai -o t u es r t ae d f e t i cmp tr T e h v d f e t e rsnai oe h tn g r n l t gp i n mb r ae r td i rnl n o ues h y ae i rn rpee tt n t o n n e f e y . f e o a dae rcse d f e t (g f ai -o t u es r poesdi a ocldf ai -o t rcso) l t g n r poesd i rnl e .l t gp i n mb r ae rcse n s-ae l t gp i poesr F ai f e y .,o n n l o n n .o n p i n mb r wl e i usdl e. o t u es ib d cse a r n l s t C mp tr ue fixed number of bits t rpee t ni e e.h cmmo l ue b - n tso i e es r 8b , 6 o ues s a o e rsn a n g rT e o t n -sd il gh frn g r ae -i 1 y te t t b , 2b o 6 -i B s e b - n tstee r t orpee tt nsh me frne es i 3 -i r 4b . ei s il gh,h r ae w e rsnai ce so i g r t t t d te o t : 1 Unsigned Integers:a rpee t eoa dp si i e es . cn e rsn zr n oi en g r t v t . 2 Signed Integers: a rpee tzr, oi e a d n g t e i e es T re rpee tt n sh me h d b e . cn e rsn eo p si n e ai n g r he e rsnai ce s a en t v v t . o po oe fr i e i e es rp sd o s n d n g r g t : a S nMa n u e e rsnai . i - g i d rpee tt n g t o b 1 C mp me te rsnai . ' o l n rpee tt n s e o c 2 C mp me te rsnai . ' o l n rpee tt n s e o Y u a tepo rmme, edt d c eo teb - n t a drpee tt nsh mefr o rne esd p n i o o , s h rga rn e o ei n h il gh n e rsnai ce d te o o yu i gr eed g n t , n y u a pct n rq i me t S p oeta y un e acu tro cu t gas l u ni f m 0u t 2 0 y u o r p lai ' e u e ns u p s h t o ed o ne fr o ni i os r . n ma q a ty r l t o p o 0 ,o mi t h oe h 8b u s n di e e sh me s h r in n g t e u esn o e . g co s te -i ni e n g r ce a tees o e ai n mb r i l d h t g t v vv

3.1 n-bit Unsigned Integers


U s n di e es a rpee t eoa dp si i e esb t o n g t ene esT e a eo a u s n di e e i ni e n g r cn e rsn zr n oi en g r u n t e ai i g r h vl f n ni e n g rs g t t v t , v t . u g t i epee a " magnitude of its underlying binary pattern" n rrtd s the t .
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 4/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Example 1: S p oeta n 8a dteb ayp t r i 0 0 0 0 B tevl o tiu s n di e e i 1 2 0 + u p s h t = n h i r at n s 1 0 0 1 ,h a e fh ni e n g rs ^ n e u s g t


126=6D ^ 5.

Example 2: S p oe h t = 6a dteb ay atr i 0 0 0 0 0 0 1 0 B te a eo tiu s n ditg r u p s ta n 1 n h i r p t n s 0 1 0 0 0 0 0 0 ,h vl fh ni e ne e n e u s g


i 123+121 =40D s ^ ^2 14.

Example 3: S p oe h t = 6a dteb ay atr i 0 0 0 0 0 0 0 0 B tevl o tiu s n ditg r u p s ta n 1 n h i r p t n s 0 0 0 0 0 0 0 0 ,h a e fh ni e ne e n e u s g


i0 s. A n-i p t r cn rpee t2 n d t c i e es A n-i u s n d i e e cn rpee ti e esf m 0 t n b at n a e rsn ^ ii t n g r n b ni e n g r a e rsn n g r r t e sn t . t g t t o o ( ^ ) 1 a tb l e b l : 2 n - ,s a u td e w a o

n
8 1 6 3 2 6 4

Mnmm iiu
0 0 0 0

Mxmm aiu
(^)1 (25 28=5) (^6- (6,3) 21)1 =555 (^2- (4249725 (+dgt) 23)1 =,9,6,9) 9 iis (^4- (1,4,4,7,0,5,1) 26)1 =8467403795165 (9 dgt) 1+ iis

3.2 Signed Integers


S n d i e escn rpee tzr, oi ei e es a w la n g t ei e es T rerpee tt nsh me ae i e n g r a e rsn eo p si n g r s e s e ai n g r he e rsnai ce s r g t t v t , l v t . o aaa lfr i e i e es vib o s n d n g r l e g t : 1 S nMa n u e e rsnai . i - g i d rpee tt n g t o 2 1 C mp me te rsnai . ' o l n rpee tt n s e o 3 2 C mp me te rsnai . ' o l n rpee tt n s e o I a tea o e he sh me,h most-significant bit ( b icldtesign bit. h s nb iue t rpee t h n l h b v tre ce ste l ms) ae h s l T e i i s sd o e rsn te g t sign o tei e e - i 0fr oi ei e es n 1fr e ai i e esT emagnitude o tei e e, o ee, fh n g r wt o p si n g r a d o n g t e n g r h t h t v t v t . fh n g rh w vri t s i epee d f e t i d f e t ce s n rrtd i rnl n i rn sh me. t f e y f e

3.3 n-bit Sign Integers in Sign-Magnitude Representation


I s nma n u e e rsnai : n i - g i d rpee tt n g t o T emot i i a tb ( b i tesign bit, i vl o 0 rpee t g p si i e e a d 1 rpee t g h s s n i n i ms)s h -g f c t wt a e f e rsni oi e n g r n h u n t v t e rsni n n g t ene e. e ai i g r v t T erma i n- b s e rsns h ma n u e( sl e a e o tene e. h a sl evl o tene e i h e in 1 i rpee t te g i d a o t vl ) fh i g rT e bo t a e fh i g rs ng t t b u u t u u t i epee a " e g i d o te n-) ib ay atr" n rrtd s t ma n u e fh ( 1- t i r p t n. t h t b n e

Example 1:u p s ta n 8a dte i r rpee tt ni 0 1 0 0 0 B S p oe h t = n h b ay e rsnai s n o 0 01.


S nb i0 p si i is g t oi e t v A sl e a es 0 0 0 B = 6 D bo t vl i1 0 0 1 u u 5 H netene e i+ 5 e c,h i g rs 6 D t

Example 2:u p s ta n 8a dte i r rpee tt ni 1 0 0 0 0 B S p oe h t = n h b ay e rsnai s n o 0 01.


S nb i1 n g t e i is g t e ai v A sl e a es 0 0 0 B = 1 bo t vl i0 0 0 1 u u D H netene e i- D e c,h i g rs 1 t

Example 3:u p s ta n 8a dte i r rpee tt ni 0 0 0 0 0 B S p oe h t = n h b ay e rsnai s n o 0 00.


S nb i0 p si i is g t oi e t v A sl e a es 0 0 0 B = 0 bo t vl i0 0 0 0 u u D
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 5/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

H netene e i+ D e c,h i g rs 0 t

Example 4:u p s ta n 8a dte i r rpee tt ni 1 0 0 0 0 B S p oe h t = n h b ay e rsnai s n o 0 00.


S nb i1 n g t e i is g t e ai v A sl e a es 0 0 0 B = 0 bo t vl i0 0 0 0 u u D H netene e i- D e c,h i g rs 0 t

T e rw ak o s nma n u e e rsnai ae h da b cs fi - g i d rpee tt n r: g t o 1 T ee r t orpee tt n (0 0 0 0 Ba d1 0 0 0 B frh n mb r eo w i cu l dt i f i c . h r ae w e rsnai s0 0 0 0 n 0 0 0 0 )o te u e zr, h h o l e o n f e y o c d a ei n c a dcnui . n o fs n o 2 P si a dn g t ene es edt b poesdsp rtl . oi e n e ai i g r n e o e rcse e aae . t v v t y

3.4 n-bit Sign Integers in 1's Complement Representation


I 1 cmp me te rsnai : n ' o l n rpee tt n s e o A a ,h mot i i a t i( b ite g i te s s n i n b ms) h sign bit, i vl o 0rpee t gp si i e es n 1rpee t g n gf c t s wt a e f e rsni oi en g r a d e rsni h u n t v t n n g t ene es e ai i g r v t . T e e in n- b s e rsns h ma n u e fh i e e,soo s h rma i n g 1 i rpee t te g i d o ten g ra flw : t t t l fr oi ene este bo t vl o tene e ie u lo" e g i d o te n-) ib ay atr" o p si i g r h a sl e a e fh i g rs q at t ma n u e fh ( 1- t i r p t n. t v t , u u t h t b n e fr e ai i e estea sl e a e fh i e e ie u lo" e g i d o te o n g t en g r h bo t vl o ten g rs q at t ma n u e fh complement ( v t , u u t h t inverse) f o te n-) ib ay atr"h ne ae 1 cmp me t h ( 1- t i r p t n (e c cld ' o l n) b n e l s e .

Example 1:u p s ta n 8a dte i r rpee tt n 0 1 0 0 0 B S p oe h t = n h b ay e rsnai n o 0 01.


S nb i0 p si i is g t oi e t v A sl e a es 0 0 0 B = 6 D bo t vl i1 0 0 1 u u 5 H netene e i+ 5 e c,h i g rs 6 D t

Example 2:u p s ta n 8a dte i r rpee tt n 1 0 0 0 0 B S p oe h t = n h b ay e rsnai n o 0 01.


S nb i1 n g t e i is g t e ai v A sl e a es h cmp me t f 0 0 0 B i. 1 1 1 B = 1 6 bo t vl ite o l n o 0 0 0 1 ,e 1 1 1 0 u u e ., 2D H netene e i- 2 D e c,h i g rs 1 6 t

Example 3:u p s ta n 8a dte i r rpee tt n 0 0 0 0 0 B S p oe h t = n h b ay e rsnai n o 0 00.


S nb i0 p si i is g t oi e t v A sl e a es 0 0 0 B = 0 bo t vl i0 0 0 0 u u D
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 6/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

H netene e i+ D e c,h i g rs 0 t

Example 4:u p s ta n 8a dte i r rpee tt n 1 1 1 1 1 B S p oe h t = n h b ay e rsnai n o 1 11.


S nb i1 n g t e i is g t e ai v A sl e a es h cmp me t f 1 1 1 B i. 0 0 0 B = 0 bo t vl ite o l n o 1 1 1 1 ,e 0 0 0 0 u u e ., D H netene e i- D e c,h i g rs 0 t

A a ,h da b cs r: g i te rw ak ae n 1 T ee r t orpee tt n (0 0 0 0 Ba d1 1 1 1 B fr eo . h r ae w e rsnai s0 0 0 0 n 1 1 1 1 )o zr. o 2 T e oi ene es n n g t ene es edt b poesdsp rtl . h p si i g r a d e ai i g r n e o e rcse e aae . t v t v t y

3.5 n-bit Sign Integers in 2's Complement Representation


I 2 cmp me te rsnai : n ' o l n rpee tt n s e o A a ,h mot i i a t i( b ite g i te s s n i n b ms)s h sign bit, i vl o 0rpee t gp si i e es n 1rpee t g n gf c t wt a e f e rsni oi en g r a d e rsni h u n t v t n n g t ene es e ai i g r v t . T e e in n- b s e rsns h ma n u e fh i e e,soo s h rma i n g 1 i rpee t te g i d o ten g ra flw : t t t l fr oi ene este bo t vl o tene e ie u lo" e g i d o te n-) ib ay atr" o p si i g r h a sl e a e fh i g rs q at t ma n u e fh ( 1- t i r p t n. t v t , u u t h t b n e fr e ai i e es tea sl evl o tei e e i e u lo" ema n u eo tecomplement o te o n g t e n g r h bo t a e f h n g rs q a t t v t , u u t h g i d fh t fh ( 1-ib ay atr plus one"h ne ae 2 cmp me t n-) t i r p t n b n e (e c cld ' o l n) l s e .

Example 1:u p s ta n 8a dte i r rpee tt n 0 1 0 0 0 B S p oe h t = n h b ay e rsnai n o 0 01.


S nb i0 p si i is g t oi e t v A sl e a es 0 0 0 B = 6 D bo t vl i1 0 0 1 u u 5 H netene e i+ 5 e c,h i g rs 6 D t

Example 2:u p s ta n 8a dte i r rpee tt n 1 0 0 0 0 B S p oe h t = n h b ay e rsnai n o 0 01.


S nb i1 n g t e i is g t e ai v A sl e a es h cmp me t f 0 0 0 Bp s ,e 1 1 1 1 B + 1 = 1 7 bo t vl ite o l n o 0 0 0 1 l 1 i. 1 1 0 u u e u ., B 2D H netene e i- 2 D e c,h i g rs 1 7 t

Example 3:u p s ta n 8a dte i r rpee tt n 0 0 0 0 0 B S p oe h t = n h b ay e rsnai n o 0 00.


S nb i0 p si i is g t oi e t v A sl e a es 0 0 0 B = 0 bo t vl i0 0 0 0 u u D H netene e i+ D e c,h i g rs 0 t
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 7/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Example 4:u p s ta n 8a dte i r rpee tt n 1 1 1 1 1 B S p oe h t = n h b ay e rsnai n o 1 11.


S nb i1 n g t e i is g t e ai v A sl e a es h cmp me t f 1 1 1 Bp s ,e 0 0 0 0 B + 1 = 1 bo t vl ite o l n o 1 1 1 1 l 1 i. 0 0 0 u u e u ., B D H netene e i- D e c,h i g rs 1 t

3.6 Computers use 2's Complement Representation for Signed Integers


Weh v d cse trerpee tt n fr i e i e es s n dma n u e 1 cmp me t n 2 cmp me t ae i usd he e rsnai s o s n d n g r i e - g i d , ' o l n a d ' o l n. s o g t :g t s e s e C mp tr ue 'cmp me tnrpee t gs n di e esT iib cue o ues s 2 o l n i e rsni i e n g r h s ea s: s e n g t . s 1 T eeio l o erpee tt nfr h n mb r eoi 2 cmp me ti ta o t orpee tt n i s n . h r s n n e rsnai o te u e zr n ' o l n, s d fw e rsnai sn i y o s e ne o g ma n u e n 1 cmp me t g i d a d ' o l n. t s e 2 P si a dn g t ei e es a b t ae tg te i a d i a dsbrco . u t co cnb cre o t . oi e n e ai n g r cn e r td o eh rn d i n n u t t n S br t n a e ar d u t v v t e t o ai ai i ui te a d i l i. s g h "d i n o c n t o g"

Example 1: Addition of Two Positive Integers: S p oe h tn 8 6 D + 5 = 7 D u p s ta = , 5 D 0


6D 5 5 D 00 00B 10 01 00 00B+ 00 11( 00 01B 10 10

7D(K 0 O)

Example 2: Subtraction is treated as Addition of a Positive and a Negative Integers: S p oeta n 8 5 u p s ht = , D


www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 8/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

-5 =6D+(5)=6D D 5 -D 0
6D 5 -D 5 00 00B 10 01 11 11B+ 11 01( 01 10B 01 10

6D(icr cry-O) 0 dsad ar K

Example 3: Addition of Two Negative Integers: S p oe h tn 8 - 5 - 5 = ( 6 D + ( 5 ) = - 0 u p s ta = , 6 D D -5) -D 7D


-5 6D -D 5 11 11B 01 11 11 11B+ 11 01( 11 11B 01 00

-0 (icr cry-O) 7D dsad ar K

B cue fh fixed precision ( . ea s o te i, . fixed number of bits)a n-i2 cmp me t i e i e e h s cr i rn e F r e , n b ' o l n s n d n g r a a et n a g . o t s e g t a ea l fr = ,h rn eo 2 cmp me t i e i e ess 1 8t + 2 . ui a d i ( dsbrco )ii xmp ,o n 8 te a g f ' o l n s n d n g r i- 2 o 1 7 D r g d i n a u t t n, s e s e g t n t o n ai t i otn t cek h te te eu ece s h rn ei oh r od, h te overflow o underflow h s curd mp r to h c w eh rh rsl xed ti a g , te w rsw eh r a t s n r a ocr . e

Example 4: Overflow: S p oe h tn 8 1 7 + 2 = 1 9 (vro - eo dte a g ) u p s ta = , 2 D D 2 D o ef w b y n h rn e l


17 2D 2 D 01 11B 11 11 00 01B+ 00 00( 10 00B 00 01

-2D(rn) 17 wog

Example 5: Underflow: S p oe h tn 8 - 2 D - 5 = - 3 D(n ef w- e wte a g ) u p s ta = , 1 5 D 1 0 u d ro b l h rn e l o


-2D 15 -D 5 10 01B 00 01 11 11B+ 11 01( 01 11B 11 10

+2D(rn) 16 wog

T eflwn d ga ep i h w te2 cmp me t ok. yr-r n i ten mb ri , a e f m - 2 t h oo i i rm xln o h ' o l n w rsB ear g g h u e le vl s r l g a as s e a n n u o 18 o + 2 ae e rsne cni o s b i oi te ar b . 1 7 r rpee td o t u ul yg r g h cr i g y n n y t

3.7 Range of n-bit 2's Complement Signed Integers


A n-i2 cmp me t i e i e e cnrpee tne esrm - ^ n 1 t + ^ n 1 - , s a u td Tk n t n b ' o l n s n d n g r a e rsn i g r f t s e g t t o 2 ( - ) o 2 ( - ) 1 a tb l e .ae oe a ta tesh mecnrpee t ltei e eswti tern e wto t n g p I oh r od,h r in mi i h t h ce a e rsn a h n g r i n h a g , i u a y a .n te w rs tee s o s n l t h h sg i e es i i te u p r drn e n g r wt n h sp ot a g . t h e

mnmm iiu

mxmm aiu
9/25

www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

-27 (-2) (^) =18

+27- (+2) (^)1 =17 +21)1(+277 (^5- =3,6) +23)1(+,4,8,4)9 (^1- =2174367(+ dgt) iis +26)1 (^3-

1 -21)(-278 6 (^5 =3,6) 3 -23)(-,4,8,4) 2 (^1 =2174368 6 -26) 4 (^3

(-,2,7,3,5,7,0) (+,2,7,3,5,7,0)1+ =9233206847588 =9233206847587(8 dgt) iis

3.8 Decoding 2's Complement Numbers


1 C ek h sign bit (e oe a S. . h c te d n td s ) 2 IS 0 te u e ip si a di a sl e a es h b ay a e fh rma i n- b s . f = ,h n mb rs oi e n t bo t vl ite i r vl o te e in 1 i. t v s u u n u ng t 3 IS 1 ten mb rsn g t e y ucu " vr ten- b sa dp s1 t g t h a sl evl o n g t e . f = ,h u e i e ai . o o l i et h 1 i n l " o e te bo t a e f e ai v d n t u u u v n mb r u e. Atrai l y u cu sa te rma i n- b s f m te r h (at i i a tb ) L o frte fs l n t e , o o l cn h e in e vy d ng 1 i r t o h i t l s s n i n i. o k o h it g e -g f c t r ocr ne f .l a te i t telt fh tit curneo 1 T ef p dp t r g e tea sl e a e cur c o 1 Fp l h b s o h e o ta fs ocr c f .h l e at n i s h bo t vl . e i l t f r e i p e v u u F r xmp , o ea l e
n=8 btpten=11000B , i atr 0 10 S=1 ngtv eaie Sann fo tergtadfi altebt t telf o tefrtocrec o 1 0110B=6D cnig rm h ih n lp l h is o h et f h is curne f 1 10 0 Hne tevlei -0 ec, h au s 6D

3.9 Exercise (Integer Representation)


1 Wh t r te a g s f -i 1 -i 3 -ia d6 -ii e e, "ni e " n "g e "e rsnai ? . a ae h rn e o 8b , 6b , 2b n 4b n g ri u s n d a d s n d rpee tt n t t t t t n g i o 2 Gv te a e f 8 0 1 1 7 a d2 5i 8b u s n drpee tt n . i h vl o 8 , , , 2 ,n 5 n -i ni e e rsnai . e u t g o 3 Gv te a e f 8 , 8 , 1 0 + , 1 8 a d+ 2 i 8b 2 cmp me t i e rpee tt n . i h vl o + 8 - 8 - , , 1 - 2 ,n 1 7 n -i ' o l n s n d e rsnai . e u t s e g o 4 Gv te a e f 8 , 8 , 1 0 + , 1 7 a d+ 2 i 8b s nma n u e e rsnai . . i h vl o + 8 - 8 - , , 1 - 2 ,n 1 7 n -i i - g i d rpee tt n e u tg t o 5 Gv te a e f 8 , 8 , 1 0 + , 1 7a d+ 2 i 8b 1 cmp me te rsnai . . i h vl o + 8 - 8 - , , 1 - 2 n 1 7 n -i ' o l n rpee tt n e u t s e o 6 [ D ] r. . T O moe O

Answers
1 T ern eo u s n d n-i i e esi [ , 2 ( - ) . h rn eo n-i 2 cmp me ts n d i e e i . h a g f ni e g b n gr s 0 t t ^ n 1 ] Te ag f b ' o l n i e n grs t s e g t [2(-) +^n1-] -^n1, 2(-)1; 2 8 ( 1 1 1 0 )0 ( 0 0 0 0 )1 ( 0 0 0 0 )1 7 ( 1 1 1 1 )2 5 ( 1 1 1 1 ) . 8 0 0 0 0 , 0 0 0 0 , 0 0 0 1 ,2 0 1 1 1 ,5 1 1 1 1 . 3 +8 (11 10)-8 (00 10)- (11 11)0 (00 00) + (00 00) -2 (00 . 8 00 00,8 11 00,1 11 11, 00 00, 1 00 01, 18 10 0 0 )+ 2 ( 1 1 1 1 ) 0 0 ,1 7 0 1 1 1 . 4 + 8 ( 1 1 1 0 )- 8 ( 1 1 1 0 )- ( 0 0 0 0 )0 ( 0 0 0 0 o 1 0 0 0 )+ ( 0 0 0 0 ) . 8 0 0 0 0 ,8 1 0 0 0 ,1 1 0 0 1 , 0 0 0 0 r 0 0 0 0 ,1 0 0 0 1 , - 2 ( 1 1 1 1 )+ 2 ( 1 1 1 1 ) 1 7 1 1 1 1 ,1 7 0 1 1 1 . 5 + 8 ( 1 1 1 0 )- 8 ( 0 0 0 1 )- ( 1 1 1 1 )0 ( 0 0 0 0 o 1 1 1 1 )+ ( 0 0 0 0 ) . 8 0 0 0 0 ,8 1 1 1 1 ,1 1 1 1 0 , 0 0 0 0 r 1 1 1 1 ,1 0 0 0 1 , - 2 ( 0 0 0 0 )+ 2 ( 1 1 1 1 ) 1 7 1 0 0 0 ,1 7 0 1 1 1 .

4. Floating-Point Number Representation


Af ai -o t u e (rel u e) a rpee t vr l g (. 3 1 ^ 8 o a eys l1 2 0 - 8 vl . l t gp i n mb ro ran mb rcn e rsn a ey a e 1 2 0 8 ) r vr ma (. 3 1 ^ 8 ) a e o n n r l u Icu a orpee t eyl g n g t en mb r- . 3 1 ^ 8 a dvr s l e ai n mb r- . 3 1 ^ 8, s t o l l e rsn vr a e e ai u e (1 2 0 8 ) n ey ma n g t e u e (1 2 0 8 )a d s r v l v
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 10/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

w l s eo a i s ae : e a zr,sl t td l l r u

A f ai -o t u e i tp ayepesdi tesi ti n tt n wt afraction ()a d a exponent () f l t gp i n mb r s y i l xrse n h c ni oai , i o n n cl e f c o h F, n n Eoa cr i radix ()i tefr o F r E D c l u es s rd o 1 ( 0 E; heb ayn mb r uerd o 2 et n a r, h om f ^ . ei n mb r ue a i f 0 F 1 ^ )w i i r u es s a i f n ma x l n x ( ^ ) F 2 E. R pee tt n o f ai p i n mb ri n tu i e F rea l te n mb r5 . 6 cn b rpee td a e rsnai o f l t g o t u e s o n u . o xmp , h u e 5 6 a e e rsne s o n n q e 5 5 6 1 ^ , . 5 6 1 ^ , . 5 6 0 3 a ds o . h f co a p r cnb normalized.nten r le fr . 6 0 1 0 5 6 0 2 0 0 5 6 1 ^ , n o n T e r t n l at a e ai I h omazd om, i tees n a i ln nzr d ib fr te a ip i .o ea l d c l u e 1 3 4 6 cnb n r le a h r io l s g o -eo it e e h rd o tF r xmp , ei n mb r 2 . 5 7 a e omazd s y ne g o x n e ma i 1 2 4 6 0 2 b ay u e 1 1 . 0 1 cnb n r le a 1 0 1 1 B 2 3 . 3 5 7 1 ^ ; i r n mb r 0 0 1 1 B a e omazd s . 1 0 1 ^ . n i Iii otn t n t ta f ai -o t u es uf f m loss of precision w e rpee tdwt a i dn mb r f tsmp r to oe h tl t gp i n mb r sf rr a o n n e o h n e rsne i fe u e o h x b s e . 2b o 6 -i.h ib cueteeaeinfinite n mb r fel u es ee wti as l a g o sy i (g 3 -i r 4b ) i s ea s h r r t ., t tT s u e o ra n mb r ( n i n ma rn e f as v h l 0 t 0 )O teoh r a d a b b ay atr cnrpee t finite 2 nd t c n mb r H ne n t l h ra . o . . n h te h n , n-i i r p t n a e rsn a 0 1 t n e ^ ii t u es e c, o a te el sn . l n mb r cnb rpee td T e ers a poi t nwl e sdi ta ,eu e i l s f cuay u es a e e rsne .h n aet p rx i ib ue n ed rsl d n o o acrc. ma o l s t s Iia oi otn t n t ta f ai n mb r rh t ivr muhl s f i t h ni e e ai mei Icu b ts l mp r to oe h tl t g u e ai mei s ey c e e ie ta n g r rh t. o l e s a o n t c s f n c t t ct d sedu wt a ocldd d ae floating-point co-processor. e c, s i e esf o r p lai d e n t e u e p e p i s-ae e i td h l c H neue n g r iy u a pct n o s o rq i t i o r f ai -o t u es l t gp i n mb r o n n . I cmp tr f ai -o t u es r rpee tdi si tin tt no fraction () n exponent () i a n o ues l t gp i n mb r ae e rsne n c ni oai f ,o n n e f c o F ad E wt radix o h f 2 i te om o F 2 E B t Ea dFcnb p si a w l s e ai . d r cmp tr a o tEE 5 s n adfr , h fr f ^ . oh n n a e oi e s e a n g t eMo en o ues d p I 7 4 t d r o t v l v E a rpee t gf ai -o t u esT eeaet orpee tt nsh me: 2b s g -rc i a d6 -id u l e rsni l t gp i n mb r h r r w e rsnai ce s 3 -i i l peio n 4b o b n o n n . o tne sn t e peio . rc i sn

4.1 IEEE-754 32-bit Single-Precision Floating-Point Numbers


I 3 -is g -rc i f ai -o te rsnai : n 2b i l peio l t gp i rpee tt n tne sn o n n o T e s s n i n b ite h mot i i a t i s h sign bit () i 0fr e ai n mb r a d1fr oi e u es gf c t S, t o n g t e u es n wh v o p si n mb r t v . T e oo i 8b s e rsn exponent () h flwn l g i rpee t t E. T e e in 2 b s e rsns h rma i 3 i rpee t fraction () ng t F.

Normalized Form
Lt i s ae i a ea l sp oe h t h 3 -ip t r i1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 , e' l t t wt n xmp ,u p s ta te 2b at n s sl r u h e t e 00 01 1 00 00 00 00 00 wt: i h S=1
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 11/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

E=10 00 00 01 F=0100 00 00 00 00 1 00 00 00 00 00 I te n h normalized form,h ataf co in r le wt a i lil d g1i te om o 1 F I tiea l te te c l at n s omazd i n mpc e i u r i i h it a n n h fr f . . h xmp ,h n s e ataf co i1 0 1 0 0 0 0 0 0 0 0 0 0 = 1 + 1 2 - + 1 2 - = 1 3 5 . c l at n s . 1 0 0 0 0 0 0 0 0 0 0 u r i ^2 ^3 .7D T es nb rpee t tes no ten mb rwt S 0fr oi ea dS 1fr e ai n mb rI tiea l wt h i i e rsns h i fh u e, i = o p si n = o n g t e u e. h xmp i g t g h t v v n s e h S 1 tiia e ai n mb ri. 1 3 5 . = ,h s n g t e u e,e - . 7 D s v ., I n r le fr teata ep n n i E 1 7 ( -ae ecs-2 o b s1 7. h i b cue w n e t n omazd om, h c l xo e t s - 2 s cld xes1 7 r i -2 ) T i s ea s e ed o i u o l a s rpee t ohp si a dn g t eep n n. t a 8b E rn i f m 0t 2 5 teecs-2 sh mecu e rsn b t oi e n e ai xo e tWi n -i ,a g g r t v v h t n o o 5 ,h xes1 7 ce ol d po i ataep n n o -2 t 1 8 I tiea l E 1 7 1 9 1 7 2 . rv e c lxo e t f1 7 o 2 . h xmp , - 2 = 2 - 2 = D d u n s e H nete u e rpee tdi- . 7 ^ = 5 5 . e c,h n mb re rsne s 1 3 5 2 2 - . D

De-Normalized Form
N r le fr h s sr u po l wt a i lil d g1fr h f co , cn o rpee t h n mb r eo omazd om a a ei s rb m, i n mpc e i i o e h it a n o te r t n i a n t e rsn te u e zr! ai t C n i e o r l nti o v c y us f h ! n eo s D -omazdfr w s ei dt rpee t eoa doh r u es en r le om a d v e o e rsn zr n te n mb r i s . F r = ,h n mb r aei ted -omazdfr A i lil d g0( s a o 1 iue fr h f co ;n te o E 0 te u es r n h en r le om. n mpc e i i it a n i t d f )s sd o te r t n a d h ne ai ataep n n ia as 1 6 H nete u e zr cnb rpee tdwt E 0a dF 0(ea s 0 0 2 - 2 = ) c lxo e ts l y - 2 . e c,h n mb r eo a e e rsne i = n = b cue . ^ 1 6 0. u w h Wecna orpee t eys l oi ea dn g t en mb r i d -omazdfr wt E 0 F r xmp , S 1 a l e rsn vr ma p si n e ai u esn en r le om i = . o ea l i = , s l t v v i h ef E 0 a dF 0 1 0 0 0 0 0 0 0 0 0 0 .h ataf co i0 0 1 1 2 - + ^ 3 0 3 5 . i e = , i = , n = 1 0 0 0 0 0 0 0 0 0 0 T e c l at n s . 1 = ^ 2 1 2 - = . 7 D S c S 1 i s u r i n t an g t en mb rWi E 0 teata ep n n i - 2 . e c ten mb rs- . 7 ^ 1 6 = - . 0 - 9 e ai u e. t = ,h c l xo e ts 1 6 H ne h u e i 0 3 5 2 - 2 v h u 441^3, w i ia et me s l e ai n mb rc s t zr) h h s n x e l ma n g t e u e (oe o eo. c r y l v l

Summary
I smmayte a e N icl l e a flw : nu r,h vl () a u td soo s u s ca l F r E 2 4 N = ( 1 ^ 1 F 2 ( - 2 ) T een mb r aei tes-ae normalized fr T e o1 5, -)S . ^ E 1 7 .h s u es r n h ocld l om. h s nb rpee t tes n o ten mb rFat n l at(. ) r n r le wt a i li l d g 1 T e i -i e rsns h i f h u e. rco a p r 1 F ae omazd i n mpc e i . h g t g i i h it a n ep n n ib so i ecs) f 2 ,oa t rpee t ohp si a dn g t eep n n.h rn eo ep n n xo e ts i (rn xeso 1 7 s s o e rsn b t oi e n e ai xo e tT e a g f xo e t a t v v i- 2 t + 2 . s 16 o 17 F r = 0 N = ( 1 ^ 0 F 2 ( 1 6 . h s n mb r aei tes-ae denormalized fr T e oE , -)S . ^ - 2 ) T ee u es r n h ocld l om. h ep n n o 2 - 2 ea ae t a ey ma n mb rD n r le fr in e e t rpee t eo(i F 0a d xo e t f ^ 1 6 vl ts o vr s l u e. e omazd om s ed d o e rsn zr wt = n u l i h E 0. cna orpee t vr s l oi e n n g t e u e c s t zr. = ) t a l e rsns ey ma p si a d e ai n mb r l e o eo I s l t v v o F r = 2 5 irpee t sei vl ssc a N (oi e n n g t enit) n N N(o a u e) h i oE 5 , e rsns p c la e,uh s I F p si a d e ai i n y a d a n t n mb r T i s t a u t v v fi . s b y n te cp o tiatl eo d h so e fh rc . s ie

Example 1: S p oeta IE-5 3 -if ai -o te rsnai p t r i0 1 0 0 0 1 0 0 0 0 0 0 0 u p s h tEE7 4 2b l t gp i rpee tt n at n s 0 0 0 0 1 0 0 0 0 0 0 to n n o e


00 00. 00 00
Sg btS=0 pstv nme in i oiie ubr E=10 00B=18 (nnraie fr) 00 00 2D i omlzd om Fato i 11B(iha ipii laig1 =1+12- +12- =17D rcin s .1 wt n mlct edn ) ^1 ^2 .5 Tenme i +.52(2-2)=+.D h ubr s 17 ^1817 35

Example 2: S p oeta IE-5 3 -if ai -o te rsnai p t r i1 0 1 1 1 1 0 0 0 0 0 0 0 u p s h tEE7 4 2b l t gp i rpee tt n at n s 1 1 1 0 0 0 0 0 0 0 0 to n n o e


00 00. 00 00
Sg btS=1 ngtv nme in i eaie ubr E=01 11B=16 (nnraie fr) 11 10 2D i omlzd om Fato i 11 (iha ipii laig1 =1+2- =15 rcin s .B wt n mlct edn ) ^1 .D Tenme i -. 2(2-2)=-.5 h ubr s 15 ^1617 07D
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 12/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Example 3: S p oeta IE-5 3 -if ai -o te rsnai p t r i1 0 1 1 1 0 0 0 0 0 0 0 0 u p s h tEE7 4 2b l t gp i rpee tt n at n s 1 1 1 0 0 0 0 0 0 0 0 to n n o e


00 00. 00 01
Sg btS=1 ngtv nme in i eaie ubr E=01 11B=16 (nnraie fr) 11 10 2D i omlzd om Fato i 10000 00 00 00 00B (iha ipii laig1 =1+2-3 rcin s .0 00 00 00 00 01 wt n mlct edn ) ^2 Tenme i -1+2-3 2(2-2)=-.000564475965(a ntb eati dcml) h ubr s ( ^2) ^1617 0500090647302 my o e xc n eia!

Example 4 (De-Normalized Form): S p oe ta I E7 4 3 -i f ai -o trpee tt n p t r i 1 u p s h t E -5 2b l t gp i e rsnai E t o n n o at n s e


0000 0000 00 00 00 00. 0000 0 00 00 00 00 01
Sg btS=1 ngtv nme in i eaie ubr E=0(nd-omlzdfr) i enraie om Fato i 00000 00 00 00 00B (iha ipii laig0 =12-3 rcin s .0 00 00 00 00 01 wt n mlct edn ) ^2 Tenme i -^2 2(16 =--4)-.0-5 h ubr s 2-3 ^-2) 2(19 141^4

4.2 Exercises (Floating-point Numbers)


1 C mp t teag s a ds ls p si n mb r ta cnb rpee tdi te 2b n r le fr . o ue h l et n maet oi e u es h t a e e rsne n h 3 -i omazd om. r l t v t i 2 C mp t teag s a ds ls n g t e u es a b rpee tdi te 2b n r le fr . o ue h l et n maet e ai n mb r cn e e rsne n h 3 -i omazd om. r l v t i 3 R p a ()o te 2b d -omazdfr . e et1 frh 3 -i en r le om. t i 4 R p a ()o te 2b d -omazdfr . e et2 frh 3 -i en r le om. t i

Hints:
1 Lret oi e u e: = , = 1 1 1 1 ( 5 ) F 1 1 1 1 1 1 1 1 1 1 1 1 . . ag s p si n mb rS 0 E 1 1 1 0 2 4 , = 1 1 1 1 1 1 1 1 1 1 1 t v S ls p si n mb rS 0 E 0 0 0 0 1 ( ) F 0 0 0 0 0 0 0 0 0 0 0 0 . maet oi e u e: = , = 0 0 0 0 1 , = 0 0 0 0 0 0 0 0 0 0 0 l t v 2 Sme s b v, u S 1 . a a a o eb t = . 3 Lret oi e u e: = , = , = 1 1 1 1 1 1 1 1 1 1 1 . . ag s p si n mb rS 0 E 0 F 1 1 1 1 1 1 1 1 1 1 1 1 t v S ls p si n mb rS 0 E 0 F 0 0 0 0 0 0 0 0 0 0 0 0 . maet oi e u e: = , = , = 0 0 0 0 0 0 0 0 0 0 1 l t v 4 Sme s b v, u S 1 . a a a o eb t = .

Notes For Java Users


Y u cn ueJ K meh d F o t i t i s o l a ( n b t ) o D u l . o g i s o o b e l n b t ) t o a s D to s l a . n B t T F o t i t i s r o b e l n B t T D u l ( o g i s o c aeas g -rc i 3 -if o to d u l peio 6 -id u l wt tesei b p t rsa dpi te rt e i l peio 2b l a r o b -rc i 4b o b e i h p ci i at n, n r t h i ne sn t e sn t h f t c e n r vl sF r xmp s a e.o ea l , u e
Sse.u.rnl(la.nBtTFot07ff); ytmotpitnFotitisola(xfff) Sse.u.rnl(obelnBtTDul(xfffffff); ytmotpitnDul.ogisoobe01ffffffL)

4.3 IEEE-754 64-bit Double-Precision Floating-Point Numbers


T e e rsnai sh me o 6 -id u l peio is l t te 2b s g -rc i : h rpee tt n ce fr 4b o b -rc i s i a o h 3 -i i l peio o t e s n mir tne sn T e s s n i n b ite h mot i i a t i s h sign bit () i 0fr e ai n mb r a d1fr oi e u es gf c t S, t o n g t e u es n wh v o p si n mb r t v . T e oo i 1 b s e rsn exponent () h flwn 1 i rpee t l g t E. T e e in 5 b s e rsns h rma i 2 i rpee t fraction () ng t F.

www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html

13/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

T e a e N icl l e a flw : h vl () a u td soo s u s ca l N r le fr F r E 2 4 , N = ( 1 ^ 1 F 2 ( - 0 3 . omazd om:o 1 i 06 -)S . ^E12) D n r le fr F r = 0 N = ( 1 ^ 0 F 2 ( 1 2 ) T ee r i te e omazdfr e omazd om:o E i , -)S . ^ - 0 2 .h s aen h d n r le om. i F r = 2 4 , rpee t sei vl ssc a N ( f i ) a (o a u e) oE 0 7 N e rsns p c la e,uh s I F i it , N n t n mb r a u nn y N .

4.4 More on Floating-Point Representation


T ee r tre at i te l t gp i rpee tt n h r ae he p r n h f ai -o te rsnai : s o n n o Te h sign bit () slep n tr ( fr oi e u es n 1fr e ai n mb r . S i e-xl aoy 0 o p si n mb r a d o n g t e u es s f a t v v ) F r h exponent ()as-ae bias (r o te E, ocld l o excess)s p lds a t rpee t ohp si a dn g t eep n n. ia pe o s o e rsn b t oi e n e ai xo e t i t v v T eb ss e a h lo tern e F r i l peio wt a 8b ep n n,h b ss1 7(r xes1 7. o h i ist t a fh a g . o s g rc i i n -i xo e tte i i 2 o ecs-2 )F r a f ne sn h t a d u lpeio wt a 1b ep n n,h b ss 0 3(r xes1 2 ) o b rc i i 1 -i xo e tte i i1 2 o ecs-0 3. e sn h t a Te h fraction ()a ocldte F (s ae h mantissa o significand) cmp sdo a i lil d gb (e r terd p i ) l l r i o oe f n mpc e i i b f e h a i o t s it a n t o x n a dtef co a b s a e terd p i )T el d gb fr omazdn mb r i 1 w i tel d gb fr n h r t n l i ( tr h a i o t h e i i o n r le u ess ; he h e i i o ai t f x n. an t i l an t d n r le n mb r i0 e omazd u ess . i

Normalized Floating-Point Numbers


I n r le fr terd p i i p cda e tefs n nzr d i e . . 7 5 0 - 3 , . 0 0 1 ^ 1 . n omazd om,h a i o ts l e f r h it o -eo it ,, 8 6 D 1 ^ 2 D 1 0 1 1 B 2 1 B i x n a t r g, g 9 F r i r n mb rteed gb ia as ,n n e n t e e rsne epcl- h svs b o s rg . o b ay u e,h l i i s l y 1 a d ed o b rpee td xli ti ae 1 i ft a e n an t w it y s t o I I E 5 'n r le fr n E 7 4 omazd om: E s i F r i l peio , E 2 4wt ecs o 1 7 H ne teata ep n n if m - 2 t + 2 . e ai o s g -rc i 1 ne sn 5 i xes f 2 . e c,h c l xo e ts r h u o 1 6 o 1 7 N gte v ep n ns r ue t rpee t ma n mb r ( 1 ) he oi e xo e t ae sdt rpee tag n mb r xo e t ae sd o e rsn s l u es< . ; i p si ep n ns r ue o e rsn l e u es l 0w l t v r (1) > .. 0 N=(1^ 1F2(-2) -)S . ^E17 F r o b -rc i , E 2 4 wt ecs o 1 2 .h ataep n n if m - 0 2t + 0 3 a d o d u l peio 1 e sn 0 6 i xes f 0 3 T e c lxo e tsr h u o 1 2 o 1 2 ,n N=(1^ 1F2(-03 -)S . ^E12) Tk n t ta nb p t r h s finite n mb r fo i t n (2 n, h hcu rpee t ae oe h t -i at n a a t e u e o cmb ai s= ^ ) i o l e rsn finite d t c n mb r I n o w c d ii t u es t sn . in t os l t rpee th infinite n mb r i teraai( e a ma rn esy 0 t 1 h snit n mb r . s o p sie o e rsn te b u esn h elx e n s l a g as . o . a i n e u es s v l 0 0 fi ) T a i n t lf ai -o t u escnb acrtl rpee td Is a ,h c ss a poi t ni ue , h h h ts o a l t gp i n mb r a e cuae e rsne .nt d te l et p rx i s sd w i , lo n n y e o ma o c l d t loss of accuracy. e so a Te h minimum a dmaximum n r le f ai -o t u es r: n omazd l t gp i n mb r ae i o n n

Peiin rcso
Snl ige

Nraie Nmn omlzd (i)


08 00H 00 00 00000 0001 E=1 F=0 , Nmn =10 2-2 (i) .B ^16 (11593 1^3) .7445 0-8

Nraie Nmx omlzd (a)


77 FFH FF FF 01111 1110 E=24 F=0 5, Nmx =11.1 217=( (a) ...B ^2 2 2-3 217 ^2) ^2 (342251^8 .083 03)
14/25

000000000000 000000000000 00000000000B 00000000000B

www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Dul obe

01 00 00 00H 00 00 00 00 Nmn =10 2-02 (i) .B ^12 (225788021 .20355704 1^38 0-0)

7E FF FF FFH FF FF FF FF Nmx =11.1 212 =( (a) ...B ^03 2 -2-2 212 ^5) ^03 (177914635 1^0) .96338217 038

Denormalized Floating-Point Numbers


IE = 0 b t h f co i n nzr,h ntevl i i d n r le fr a dal d gb o 0i asme , s f , u te r t n s o -eo te h a e s n e omazd om, n ai u i e i i f s su d a an t flw : oo s l F r i l peio , = 0 o s g -rc i E ne sn , N=(1^ 0F2(16 -)S . ^-2) F r o b -rc i , = 0 o d u l peio E e sn , N=(1^ 0F2(12) -)S . ^-02 D n r le fr cn rpee tvr s ln mb r c sd t zr, n zr,w i cn o b rpee td i e omazd om a e rsn ey ma u es l e o eo a d eo h h a n t e e rsne n i l o c n r le fr a so ni te b v f ue omazd om,s h w n h a o e i r. i g T e n m a dmai m o denormalized floating-point numbers ae h mimu n i x mu f r:

Peiin rcso
Snl ige

Dnraie Dmn eomlzd (i)


00 00H 00 01 00000 000000000001 0000 00000000000B E=0 F=000000000001 , 00000000000B Dmn =00.12-2 =1 (i) ... ^16 2-32-2 =2-4 ^2 ^16 ^19 (141^4) . 0-5

Dnraie Dmx eomlzd (a)


07 FFH 0F FF 00000 0000 111111111111 11111111111B E=0 F= , 111111111111 11111111111B Dmx =01.12-2 = (a) ... ^16 (-^2)2-2 12-3^16 (115921^3) .744 0-8

Dul obe

00 00 00 00H 00 00 00 01 Dmn =00.12-02=1 (i) ... ^12 2-22-02=2-04 ^5 ^12 ^17 (491^34 . 0-2)

01 FF FF FFH 0F FF FF FF Dmx =01.12-02= (a) ... ^12 (-^5)2-02 12-2^12 (440477142 .51710403 1^38 0-0)

Special Values
Zero: eocn o b rpee tdi ten r le fr a dmut erpee tdi d n r le fr wt E 0a d Z r a n t e e rsne n h omazd om, n i s b e rsne n e omazd om i = n i h F 0 T ee r t orpee tt n fr eo + wt S 0a d- wt S 1 = .h r ae w e rsnai so zr: 0 i = n 0 i = . o h h Infinity: h vl o +nit (g 1 0 a d- f i (g - / ) r rpee tdwt a ep n n o a 1 ( = 2 5 T e a e f i n y e . / ) n i it e . 1 0 ae e rsne i n xo e t f l ' E u f i ., n n y ., h ls 5 fr i l peio a dE = 2 4 fr o b -rc i ) = ,n S 0( r I F a dS 1( r I F. o s g -rc i n ne sn 0 7 o d u l peio , 0 a d = f + N ) n = f - N ) e sn F o o Not a Number (NaN): a d n tsavl ta cn o b rpee tda ra n mb r e . / )N Ni rpee td N N e oe a e h t a n t e e rsne s el u e (g 0 0. a s e rsne u .
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 15/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

wt Ep n n o a 1 ( = 2 5fr i l peio a dE = 2 4 fr o b -rc i ) n a y o -eof co . i xo e t f l ' E h ls 5 o s g -rc i n ne sn 0 7 o d u l peio a d n n nzr r t n e sn ai

5. Character Encoding
I cmp trme r, h rc r r "no e "(r" pee td) s g acoe " aatre cd g sh me"( a n o ue moy caat ae e cd d o r rsne " ui e e n h sn c rc h e no i ce s a n k " aatr e"" ast " aatr p, rcd p g " c rc st c r " c rc ma "o " e a e) h e , h e, h e o . F r xmp , A CIa w l s ai , n o ea dma y te caatr es o ea l i S I( e a Lt 1 U i d ,n en s l n c n oh r h rc st : e ) cd n mb r 6 D ( 1 )t 9 D ( A )rpee t ' 't ' ' rset e . o e u es 5 4 H o 0 5 H e rsns A o Z ,ep cvl i y cd n mb r 9 D ( 1 )t 1 2 ( A )rpee t ' 't ' ' rset e . o e u es 7 6 H o 2 D 7 H e rsns a o z ,ep cvl i y cd n mb r 4 D ( 0 )t 5 D ( 9 )rpee t ' 't ' ' rset e . o e u es 8 3 H o 7 3 H e rsns 0 o 9 ,ep cvl i y Iii otn t n t ta terpee tt nsh memut ek o nb fr ab ayp t r cnb i epee . ., tsmp r t o oe h t h e rsnai ce a o s b n w e e i r at n a e n rrtd E . o n e t g te -ip t r "1 0 0 1 B cu rpee t n ti u d rh snk o no lt te esne cd di h 8b at n 0 0 0 0 " o l e rsn a yh g n e te u n w n o h p r t e d n y o no e t . T emot o h s cmmo l ue caatr no i sh me ae 7b A CII / C6 6 a d8b Lt - ( O I 8 5 -) n -sd h rc e cd g ce s r: -i S I( O I 4 ) n -i ai xI / C 8 9x y e n t S E t n S E fr etr e rp a caatr a dU i d ( O I 1 6 6 frnen t n lai ( 8 ) o w s n uo en h rc s n n o e I / C 0 4 )o i rai azt n i n. e e, c S E t o i o 1 A7b e cd gsh me( c a A CIcnrpee t 2 caatr a dsmb l A 8b caatr no i sh me -i no i ce t n s h s S I a e rsn 1 8 h rc s n y o . n -i h rc e cd g ce u ) e s t e n ( c a Lt -)a rpee t 5 caatr a dsmb l w ees 1 -ie cd gsh me sc a U i d U S2 s h s ai xcn e rsn 2 6 h rc s n y o ; h ra a 6b no i ce ( h s n o e C -) u n e s t n u c cnrpee t 6 , 6caatr a dsmb l a e rsns 5 3 h rc s n y o . 5 e s

5.1 7-bit ASCII Code (aka US-ASCII, ISO/IEC 646, ITU-T T.50)
A CIA ra Sa d r C d frnomai Itr a g ) o e fh er r h rc r o i sh me. S I(mei n tn ad o e o Ifr t n nec n e i n o te al caat cd g ce s c o h s i e e n A CIi oin l a 7b cd .I h sb e etn e t 8b t b t rui e te 8b cmp trme r SI s ri l g ay -i o e t a en x d d o -i o et tz h -i o ue t e t e l i t moy og n ai . h 8hb w s r i l ue fr ra i t n ( e t-i a oin l sd o parity check i te alcmp tr) z o T t g ay n h er o ues y . C d n mb r 3 D ( 0 )t 1 6 ( E )ae r tb (i l a l caatr a tb l e : o e u es 2 2 H o 2 D 7 H r pi a l d p yb )h rc s s a u td n e sa e e a

Hx e 2 3 4 5 6 7

0
S P 0 @ P ` p

1
! 1 A Q a q

2
" 2 B R b r

3
# 3 C S c s

4
$ 4 D T d t

5
% 5 E U e u

6
& 6 F V f v

7
' 7 G W g w

8
( 8 H X h x

9
) 9 I Y i y

A
* : J Z j z

B
+ ; K [ k {

C
, < L \ l |

D
= M ] m }

E
. > N ^ n ~

F
/ ? O _ o

C d n mb r 2 ( 0 )ite o e u e 3 D 2 H s h blank o space caatr r h rc . e ' 't ' ' 3 H 3 H ( 0 1 0 0 B t 0 1 1 0 B o ( 0 1 x x Bw ee x xitee u a n i e e 0 o 9 : 0 - 9 0 1 0 1 o 0 1 0 1 ) r 0 1 x x h r x x s h q i l tn g r ve t vl ) ae u ' 't ' ' 4 H 5 H ( 1 1 0 0 B t 0 0 1 1 B o ( 1 x x x B . A t ' 'aecni o s i o t A o Z : 1 - A 0 0 0 1 o 1 1 0 0 ) r 0 0 x x ) ' ' o Z r o t u u wt u n h gp a. ' 't ' ' 6 H 7 H ( 1 0 0 0 B t 0 1 1 1 B o ( 1 x x x B . A t ' 'aea ocni o s a o z:1-A 01 01 o 11 00) r 01 xx) '' o Z r l otuu s n wto t a . o ee,h r iag pb t enu p r s a dl ecs l tr T cn et ew e u p r n i u g p H w vrtee s a ew e p ec e n o r e e es o o vr b t en p e a d h a w a t . l ecs,i te a e f i5 o r ef h vl o b -. w a l p u t C d n mb r 0 ( 0 )t 3 D ( F ) a d1 7 ( F )aesei cnrlh rc r w i aen npi a l o e u es D 0 H o 1 1 H , n 2 D 7 H r p c l o t caat s h h r o -r tb a o e, c n e (o -i l a l, s a u tdb l . n o tee h rc r w r ue i teer d y frrnmi i cnrl n nd p yb )a tb l e e w Ma y fh s caat s ee sd n h al as o t s s o o t sa e a o e y a sn o (g SX EX a dpi e cnrle . om-ed, h haen w o sl eT erma i men gu cd s o a e . T ,T ) n r tr o t (g F r Fe )w i r o bo t.h e in ., n o ., c e n g a i flo e td y n
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 16/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

ae r: 0 Hfr a (\ '. 9 o Tb ' t ) 0 Hfr i -edo n wi ( , \ ' a d0 Hfr ar g -eun(R ' ', h hae sda line delimiter A o L eFe r e le L ' n ) n D o C ra eR tr C , r )w i r ue s n n F i c ( aline separator, a k end-of-line)o tx f s T eei u fr n tl n s n adfri d l trU i sue fr et i . h r s not ae o t d r o le emi : n e s l e u y a n i e x 0 H ( n)Wi o sue0 0 H ( r n)Mas s 0 H ( r)Po rmmi l g a e sc a CC + Jv A " " n w s D A " \" \ , d \ , c ue D " " rga n a u g s uh s /+ / a \ . g n a (h hw s rae o U i ue A ( n) w i a c td n n ) s 0 H " " c e x \ . I po rmmi l g a e sc a CC + Jv,n - e (A )sd n tda ' n , ar g - tr (D ) s n rga n a u g s uh s /+ / a lef d 0 H i e oe s \ ' cra er un 0 H a g n a i e i e ' r ,a (9 ) s \ ' \ ' tb 0 H a ' t .

DC E
0 1 2

HX E
0 0 0 1 0 2

Maig enn
NL Nl U ul SH Sato O tr f Haig edn SX Sato Tx T tr f et

DC E
1 7 1 8 1 9

HX E
1 1 1 2 1 3

Maig enn
D1 Dvc C eie Cnrl1 oto D2 Dvc C eie Cnrl2 oto D3 Dvc C eie Cnrl3 oto

3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6

0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 A 0 B 0 C 0 D 0 E 0 F 1 0

EX Edo Tx T n f et ET Edo O n f Tasiso rnmsin EQ Eqiy N nur

2 0 2 1 2 2

1 4 1 5 1 6 1 7 1 8 1 9 1 A 1 B 1 C 1 D 1 E 1 F

D4 Dvc C eie Cnrl4 oto NK Ngtv A eaie Ak c. SN Sn.Il Y yc de EB Edo T n f Tasiso rnmsin CN Cne A acl E M Edo n f Mdu eim

AK Akoldeet 2 C cnwegmn 3 BL Bl E el B S H T L F V T F F C R S O S I 2 4

Bc Sae'b 2 ak pc \' 5 Hrzna Tb oiotl a 't \' Ln Fe 'n ie ed \' Vria Fe etcl ed Fr Fe '' om ed f 2 7 2 8 2 9 2 6

SB Sbttt U usiue EC Ecp S sae I4 Fl S ie Sprtr eaao I3 Gop S ru Sprtr eaao I2 Rcr S eod Sprtr eaao I1 Ui S nt Sprtr eaao

Crig Rtr 3 arae eun 0 'r \' SitOt hf u SitI hf n 3 1

DE Dtln Ecp 17 L aaik sae 2

7 F

DL Dlt E eee

5.2 8-bit Latin-1 (aka ISO/IEC 8859-1)


I / C 8 5 ia S I -8 9 s collection o 8b caatr no i s n ad frh w s r l g a e. O E f -i h rc e cd g t d rso te et n a u g s t e n a e n I / C8 5 -,k Lt a h b t o 1 o Lt - i sotitemot o S I 8 91 aa ai l a e N . , r ai 1 n h r s h O E n p n , s cmmo l ue e cd gsh me o w s r n -sd no i ce fr et n y n e e rp a l g a e.t a 1 1pi a l caatr f m tel i sr tw i cvr l g a e leE gs, ema , uo en a u g s Ih s 9 r tb h rc s r h a n cp, h h o esa u g si n lh G r n n n e e o t i c n k i
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 17/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

I ln P r g eea d S a i . ai 1 i b cw r cmp t l wt te7b U - S Icd . h ti tefs 1 8 t i , ot u s n p n h Lt - s ak ad o aie i h -i SA CI o e T a s h it 2 aa u s n b h t , r caatr i Lt - ( d n mb r 0t 1 7(F ) itesme s SA CI o en mb r 1 8(0 )o1 9(F ) r h rc sn ai 1 c e u es o 2 7 H)s h a a U - S I C d u es 2 8 H t 5 9 H ae e n o , . n t si e . o e u es 6 (0 )o2 5( H ae i na flw : o as n d C d n mb r 1 0 A H t 5 F ) r g e soo s g F v l

Hx e A B C D E F

0
NS BP

D
SY H

I / C 8 5 h s 6p r . ei s h mot o S I -8 9 a 1 at B s e te s cmmo l ue P r1 P r2imen fr e t luo en(os, zc, O E s d n -sd at , at s a to C nr E rp a P lh C eh y a i H n ai , t , at fr o t E rp a ( ri , t , at fr ot E rp a ( tn n Lti , t , at fr u g r n ec P r 3 o S uh uo en T k h ec P r 4 o N r uo en E o i , av n ec P r 5 o a ) u s ) h s a a ) C ri P r6fr rb , at fr re, at fr e rw P r9fr uk h P r1 fr od , at 1fr h i at yic at o Aa i P r7 o G ek P r8 o H be , at o T ri , at 0 o N ri P r1 o T a P r l, l c s c , 1 w sa a d n P r 1 fr a i R P r 1 fr e i P r 1 fr rnh F n h ec P r 1 fr o t-atr 2 a b n o , at 3 o B l i at 4 o C l , at 5 o Fe c, i i , t at 6 o S uhEs n t m, c t c ns . e E rp a . uo en

5.3 Other 8-bit Extension of US-ASCII (ASCII Extensions)


B s e h s n ad e I -8 9xtee r ma y -iA CI x ni sw i ae o cmp t lwt ec oh r ei te t d ri d S 8 5 -,h r ae n 8b S Iet s n, h h r n t o aie i ah tes d a z O t e o c b h . ANSI (mei nN t n l tn ad Istt)aaWindows-1252, rWi o sC d p g 1 5 )frLt a h b t A ra ai a Sa d rs ntue ( c o i k o n w o e a e 2 2:o ai l a es d n p ue i tel ayD SWi o sss msI iasp r t fS -8 91wt cd n mb r 1 8(0 ) o1 9(F ) sd n h e c O / n w yt .ts u es o I 8 5 - i o e u es 2 8 H t 5 9 H g d e e O h as n d t d p yb caatr sc a " r s g -u tsa d d u l q oe. cmmo po l i w b si e o i l a l h rc s uh s s t i l q oe n o b -u ts A o g sa e e, ma " n e e n rb m n e e bow r ita a te u ts n a ot p e (rd cdb " r q oe"nsmeMioot ot ae w r rp cd rs ess h t l h q oe a d p s o h spo ue y s t u ts i o l r ma c sfsf r) ee e l e r w a wt q et n mak o sme s a g smb l I i b cue te d cme ti l ee a I -8 91 ( s a o i u so h i rs r o t n e y o . t t ea s h ou n s a ld s S 8 5 - i t d f r s bl O ne Wi o s1 5 )w eetee o en mb r aeu d f e . s mo enbo sr a dema ce t t a castS n w -2 2, h r h s cd u es r n en d Mot d r rw es n - i lns r t h r I d i li e e O 8 5 - a Wi o s1 5 i od roacmmo ae uhmia en . 8 91 s n w -2 2 n re t co d d t sc s b lg l i

Hx e 8 9

EBCDIC ( tn e B ay o e D c l tr a g C d ) sdi te alIM cmp tr E e d d i r C d d ei I ec n e o e U e n h er B o ues x n ma n h : y .

5.4 Unicode (aka ISO/IEC 10646 Universal Character Set)


B fr U i d , os g caatr no i sh mecu rpee t h rc r i a l g a e. o ea l w s r e e n o e n i l h rc e cd g ce o c ne e n o l e rsn caat sn l a u g sF r xmp , et n d e ln e e e rp a ue svrl no i sh me ( teI -8 9xf l. vn as g l g a eleC i s h saf uo en ss eea e cd g ce s i h S 8 5 - a y Ee n n O mi) i l a u g i h ee a ne n k n e w e cd gsh me (B 3 2G K BG ) n e cd gsh me aencnlt f ahoh ri. h sme o e u e no i ce sG 2 1 / B , I5. n Ma y no i ce s r i o f o ec te,e te a cd n mb r n i c ., ias n dt d f e t h rc r s si e o i rn caat s g f e e. U i d a t po i as n adcaatr no i sh me w i iu i r l f i tu i r a du a i o s n o e i o rv e t d r h rc e cd g ce , h h s n es , f e , n om n n mb u u. c ms d a e n c v a ei n c f g U i d s n ad i ma ti d b a n npoi og n ai cld te U i d C not m ( w wu i d . g. n o e t dr s i a e y c a n n o -rf ra i t n ae h n o e o sru @ w . c e r) t z o l c i no o U i d ia I / Cs n ad1 6 6 n o es n S I t d r 0 4 . c O E a U i d ib cw r cmp t lwt te7b U - S Ia d8b Lt - ( O 8 5 -)T a i tefs 1 8caatr n o e s ak ad o aie i h -i SA CI n -i ai 1 I -8 91. h ts h it 2 h rc s c b h t t n S , r e ae h sme s SA CI n te it 5 caatr ae h sme s ai 1 r te a a U - S I a d h fs 2 6 h rc s r te a a Lt -. ; r e n
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 18/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

U i d oin l ue 1 b s( ldU S2 o U i d C aatrSt 2 b t)w i cnrpee t p t 6 , 6 n o e r i l ss 6 i c l C - r n o e h rc c g ay t ae c e eye h h a e rsn u o 5 3 , c 5 caatr Ih s i eb e ep n e t moeta 1 b scr nl s n s t 1b sT ern eo tel a cd sn h rc s t a s c en xa d d o r h n 6 i,ur t t d a 2 i. h a g fh e lo e i e. n t e y a t g I / C 1 6 6in w f m U 0 0 H t U 1 FFH (1b so a o t mio caatr , o ei a cr n a d S I 04 s o r O E o + 0 0 o + 0 FF 2 i r b u 2 l n h rc s cvr g l ur t n t l i e) n l e a c n h tra sr t T e oin l 6b rn eo U 0 0 H t U FFH (5 3 caatr i k o n a Basic ni t ioi l cps h r i 1 -i a g f + 0 0 o + FF 6 5 6 h rc s s n w s e s c i . ga t e) Multilingual Plane (MP, o ei a tema rl g a e i uecr nl T ecaatr o td B B )cvr g l h n l j a u g s n s ur t . h h rc s us e MP aecld o n e y e i r ae l Supplementary Characters, h hae o f q e t -sd w i r n tr u nl ue . c e y U i d h s w e cd gsh me: n o e a t o no i ce s c n UCS-2 (n esl h rc r e -2B t)U e 2b ts(6b s cvr g6 , 6caatr i teB . MPi U i r C aat St v a e ye ss ye 1 i) o ei 5 3 h rc s n h MP B : t, n 5 e s sf i to mot fh a pct n. C - in wo sl e uf e fr s o te p lai sU S2 s o bo t. in c i o e UCS-4 (n esl h rc r e - B t) ss b ts3 b s cvr gB a dte u p me tr caatr U i r C aat St 4 ye U e 4 ye (2 i) o ei MP n h sp l nay h rc s v a e : t, n e e.

5.5 UTF-8 (Unicode Transformation Format - 8-bit)


T e1 /2b U i d (C -/)sgos i f i tfh d cme t o ti ma l A CI h rc r b cueec h 63 -i n o e U S24 i rsl n f e ite ou n cna s iy S Icaat s ea s ah t c y ei n c n n e, caatr cu i t ob tso s rg . ai l l gh e cd gsh me, uh a U F8 w i ue 14 b tst h rc ocp s w ye f t a e V r b - n t no i ce s sc s T-, h h ss - ye o e e o a ee n c rpee t caatrw s ei dt i rv tee ie c. U F8 te 2 cmmo l ue U - S Icaatr ue e rsn a h rc , a d v e o mpo e h f i yI T-,h 1 8 o e s f n n c n -sd SA CIh rc s s y e o l1b t, u smeescmmo lcaatr ma rq i u t 4b tsO ea,h e ie c i rvdfr ou n n y yeb t o l -o s n h rc s y e u e p o ye. vrlte f i ympo e o d cme t y e r l f n c cna i ma lU - S Itx . o tin n g iy SA CIet n s T e rnfr t nb t enU i d a dU F8ia flw : h t s mai ew e n o e n T- s soo s a o o c l

Bt is
7 1 1 1 6 2 1

Uioe ncd
0000 0000 0xxx xxxx 000y 00yy yxxx yxxx zzyy zzyy yxxx yxxx 00uu 0uuu zzyy zzyy yxxx yxxx

UF8Cd T- oe

Bts ye
(SI) ACI

0xxx 1 xxxx 10yy 1xxx 2 1yyy 0xxx 11zz 1yyy 3 10zz 0yyy 1xxx 0xxx 110u 1uzz 4 11uu 0uzz 1yyy 1xxx 0yyy 0xxx

I U F8 U i d n mb r cr so d gt te7b A CI h rc r aep d e wt al d gzr;h s a te n T-, n o e u es or p n i o h -i S Icaat s r a d d i c e n t e h e i eo tu h s h an sme a ea A CI e c, T- cnb ue wt a sf ae s gA CI n o en mb r o 1 8a da o ew i a vl s S I H ne U F8 a e sd i l ot r ui S I U i d u es f 2 n b v, h h u . h l w n . c c ae l sf q e t ue , r e cd d ui moe b ts(- b ts U F8 g n rl rq i sl ss rg a d i r e r u nl sd ae no e s g s e y n r ye 24 ye) T- e eay e u e e t a e n s . l r s o cmp t l wt A CIT eda b c o U F8imoepoes gp w r ed dt u p c tecd d et i vr b o aie i S I h rw ak f T- s r rcsi o e n e e o n ak h o e u o t ai l b h . n s ae l gh U F8ite s p p l fr to U i d . e t. T- s h mot o u roma fr n o e n a c N ts oe: U F8ue 13b tso tecaatr i B (6b )a d4b ts o sp l nay h rc r o td B (1 T- ss - ye frh h rc sn MP 1 -i, n e t ye fr u p me tr caat s us e MP 2 e e i b) i. t T e 2 A CIh rc r (aiLt l tr d i, n p ntai s n) s o e ye Mot uo ena dMi l h 1 8 S Icaat s b s ai e es it a d u c t n i sue n b t. s E rp a n e c n t , gs u o g de d Es caatr uea2b t sq e c, h hi l e etn e Lt l tr (i t e marn aue gaea d at h rc s s e -ye e u ne w i n u s x d d ai e es wt i , c , ct, rv n c cd e n t h l d o oh r ce t , re, r n n H be , rb , n oh r C i s,a a eea dK ra (J) s treb t te acns G ek Ame i , e rw Aa i a d tes h ee Jp n s n oen CK ue he-ye ) a c . n sq e cs e u ne. Alh b tsecp te1 8A CI h rc r h v al d g' 'b . oh r od,h A CI ye, i aed g l e ye,xe th 2 S Icaat s ae e i 1 i I te w rste S Ib tswt l i t e, an tn h an ' 'b ,a b i ni da dd cd desy 0 i cn ed ti n eo e ai. t e f e l Example: (ncd:6AH57H Uioe 08 9D)

Uioe(C-)i 6AH=01 00 1 110B ncd US2 s 08 10 00 0 000 UF8i 1101 1001 1110B=E 8 AH T- s 1010 0000 0000 6 2 8
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 19/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Uioe(C-)i 57H=00 10 0 110B ncd US2 s 9D 11 01 1 111 UF8i 1100 1100 1110B=E A BH T- s 1011 0011 0111 5 5 D

5.6 UTF-16 (Unicode Transformation Format - 16-bit)


U F1 ia ai l l ghU i d caatr no i sh me w i ue 2t 4b tsU F1 in t o T-6 s vr b - n t n o e h rc e cd g ce , h h ss o ye. T-6 s o cmmo lue . a ee c e n c n sd y T e rnfr t ntb ia flw : h t s mai a l s soo s a o o e l

Uioe ncd
xxxx xxxx xxxx xxxx 00uu zzyy 0uuu zzyy yxxx yxxx (uu) uuu0

UF1 Cd T-6 oe
ecdn noig

Bts ye

Sm a US2-n 2 ae s Co 101w wzzy 4 110w wzzy 101y yxxx 111y yxxx (ww=uuu-1 ww uu )

Tk n t ta fr h 6 5 6caatr i B ,h U F1 itesmea U S2( b ts H w vr4b ts r ue ae oe h to te 5 3 h rc sn MP te T-6 s h a e s C - 2 ye) o ee, ye ae sd . frh sp l nay h rc r o td te MP o te u p me tr caat s us e h B . e e i F r MPcaatr U F1 itesmea U S2 F r u p me tr caatr ec caatr e u e ap i1 -i oB h rc s T-6 s h a e, s C -. o sp l nay h rc s ah h rc rq i s a 6b e e, e r r t vl stefs f m te i -urg ts a g , u 8 0 \ D F ) h scn f m teo -urg ts a g (u C 0 a e,h itr h h hsr ae rn e( D 0 - u B F, e eo d r u r o g o \ t o h l sr ae rn e \ D 0 w o \DF) u F F.

5.7 UTF-32 (Unicode Transformation Format - 32-bit)


Sme s C -, h hue 4b tso ec caatr u e cd d a a U S4 w i ss ye fr ah h rc - n no e . c e

5.8 Formats of Multi-Byte (e.g., Unicode) Text Files


Endianess (or byte-order) : o amu i yecaatry un e t tk cr o teod r fh b tsns rg .n Fr l b t h rc , o ed o ae ae fh re o te ye i t a e I t e o
big endian,h mot i i a t yeis rda teme r l ai wt tel et d rs (i b t fs .nlittle te s s n i n b t s t e t h gf c o moy o t n i h o s a des b ye it I c o h w g r) endian,h mot i i a t yeis rda te moyoai wt te i et d rs ( t b t fs . o ea l te s s n i n b t s t e t h me r l t n i h h h s a des l l ye it F r xmp , gf c o c o h g ie t r) e (i U i d n mb r f 0 8 )ss rda 6 A i b e d n a ds rda A 6 i l l e d n B e d n wt n o e u e o 6 A H i t e s 0 8 n i n i ; n t e s 8 0 n i e n i . i n i , h c o g a o t t a g a w i po ue a r ra a lh x u , moe o h h rd cs moe ed b e d mp i r cmmo l ue ,n iotnte e u . c e s n -sd a d s f h d f l y e at

BOM (Byte Order Mark) : O i asei U i d caatrh v g cd n mb ro F F H w i i ue t B M s p c l n o e h rc a c e ai o e u e f E F , h h s sd o n c


d f e t t b -n i a dl l e d n F r i e d n B M a p as s E F Hi tes rg . o l l e d n B M i rni e i e d n n i e n i . o b -n i , O p er a F F n h t a e F ri e n i , O f e a g a t - a t g a o t - a t a p as s F F H U i d rsre tee w cd n mb r t pee ttrm c si wt a oh r h rc r p er a F E . n o e eevs h s t o o e u es o rvn if c o r h g i n te caat . a n h e U i d tx f s o l tk o tee omas n o e eti cu ae n h s fr t c l e d : B E d n U S2 EU F1 B , T-2 E i n i : C -B , T-6 EU F3 B . g a L t E d n U S2 EU F1 L, T-2 E il n i : C -L, T-6 EU F3 L. te a U F1 wt B M. h fs caatr fh f iaB M caatrw i sei stee d n s. o b -n i , T-6 i O T e it h rc o te i s O h rc , h h p ci h n i es F r i e d n h r e l e e c f e a g a B M a p as s E F Hi te trg .o l l e d n B M a p as s F F H O p er a F F n h s a eF ri e n i , O p er a F E . o t - a t U F8f ia as trda b e d n B M p y n p r H w vri sme ytmsi p rcl Wi o s a O i T- i s l y s e s i n i . O l e w o g a l s o at o ee, o ss a . n e ( atu r n w ) B M s n i a d , a d da tefs caatrnteU F8f a tes n tr t i ni tef a U F8e cd d T eB M caatr d e s h it h rc i h T- i s h i aue o d ty h i s T- no e . h O r e l e g e t l e h rc e (E F ) e cd di U F8a E B B . d i a O a te it h rc r fh f in teo F F H i no e n T- s F B F A d g B M s h fs caat o te i s o rcmme d d a ima s n r e l e n e ,st y b i orcynepee i oh r ytm. o cnh v a T- f wto t O en r t i rrtd n te ss c el t e Y u a ae U F8 i i u B M. l h e

5.9 Formats of Text Files


Line Delimiter or End-Of-Line (EOL) : o t sw e y uuete n o s oe a t o e a eti ( etdi S mei , h n o s h Wi w N tP d o p n tx f c ae n me d l r e
U io Ma)a tei s r jn dtg te. h ib cued f e t p rt gp t r ued f e t h rc r s h n r c l h le aeo e o eh rT i s ea s i rn o eai l f ms s i rn caat a te x ,l n i s f e n ao f e e
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 20/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

s-ae line delimiter (r ocld l o end-of-line o E L T on npi a lcnrlh rc r aen o e : A (n -edo L) r O ) w o -r tb o t caat s r i l d 0 H L eFe r F . n e o e vv i a d0 H(ar g -euno C ) n D C ra eR tr r R. i Wi o s O ue O 0 H(R L,\ \ "a E L n w / S ss D A C + F"r n) s O . d D U i s s 0 H( ,\ "o l n e ue A L "n) n . x F y Ma ue 0 H(R "r) n . c ss D C ,\ "o l y

End-of-File (EOF) : O O] [ D T

5.10 Windows' CMD Codepage


C aatr no i sh me( ast i Wi o ss ae codepage.nC h rc e cd g ce c r )n n w icld e n h e d l I MD se, o cni u cmma d" h p t hlyu a s e o l s n cc" o d p y h cr n cd p g , r c c c d p g - u b r t ca g te o e a e i l te ur t o e a eo " h p o e a e n m e " o h n e h cd p g . sa e Tk n t ta: ae oe h t T e e u cd p g 4 7(sdi te r i l O ) a 8b caatr e cldExtended ASCII, h hid f e t h d f l o e a e 3 ue n h oin D S i n -i h rc st ae at ga s t e l w i s i rn c f e f m Lt - fr o e u es b v 1 7 r o ai 1 o cd n mb r a o e 2 . n C d p g 1 5 ( n o s1 5 )in t xcytesmea Lt -.t si s o en mb r 0 t 9 H t l tr o e a e 2 2 Wi w -2 2, o eat h a d s l s ai 1 Ias n cd u e 8 H o F o e es n g t a dp ntai ,uha s rs g -u ts n d u l q oe. cmmo po l i bo srh t i l q oe n u c t n sc s mat i l q oe a d o b -u tsA o u o ne e n rb m n rw e ta d p y u ts e sa a da ot p enq et nmak o b xss ea s tep g isp oe t b Wi o s1 5 , u mia ee a n p s o h i u so r i rs r oe ib cue h a es u p sd o e n w -2 2 b t s b ld s d l l I -8 91 S 8 5 -. O F rnen t n lai a dci s caatr e:o e a e6 0 1fr T8 cd p g 1 0 fr C -B ,o e a e o i rai azt n n h ee h rc stcd p g 5 0 o U F ,o e a e 2 1 o U S2 Ecd p g t o i o n e 1 0 fr C -L,o e a e 3 fr h ee h rc r i G 2 1 ,o e a e 5 fr h ee h rc r i B 5 2 0 o U S2 Ecd p g 9 6 o ci s caat sn B 3 2 cd p g 9 0 o ci s caat sn i . n e n e g

5.11 Chinese Character Sets


U i d sp ot a l g a e,nl i ai l g a e le C i s (oh s le a d t d i a caatr , n o e u p r l a u g s i u n s n a u g s i h ee b t i i d n r i n l h rc s c s l n cd g a n k n mpf i at o e) Jp n s a dK ra ( lcvl cldCK. h r aemoe h n2 , 0CKcaatr i U i d . n o ecaatr a a ee n oen c l t e ae J)T ee r oe i y l r ta 0 0 J h rc sn n o eU i d h rc s 0 e c c e aeotne cd di te T- sh me w i u fr n tl rq i s b tso ec CKcaatri ta o 2b ts r f e no e n h U F8 ce , h h not ae ,e u e 3 ye fr ah J h rc , s d f ye c u y r e ne i te n no e U S2(T-6. n h u e cd d C - U F1 ) Wos sltee r a ovr u ci s caatr esw i in t o aie i U i d : r t,h r ae l ai s h ee h rc st h h s o cmp t lwt n o e e i l s o n e , c b h c G 2 1 /B : r B 3 2G K f simplified ci s caatr G 2 1 ue 2b tso ec ci s caatrT emot i i a t o h ee h rc s B 3 2 ss ye fr ah h ee h rc .h n e. n e ss ni n gf c b ( B o b t b ts r sto1t c-x t i 7b A CI i teMS o 0 T eeae b u 6 0 caatr i MS ) f oh ye ae e t t o oei wt -i S Iwt h s h t h B f .h r r a o t 7 0 h rc s e. G Kia etni o G 2 1 , h hi l e r caatr a w l s rd i aci s caatr B s n x s n f B 3 2 w i n u moe h rc s s e a t i n lh ee h rc s e o c cd e l at o n e. BG :o traditional ci s caatr BG a oue 2b tso ec ci s caatrT emot i i a t io I5 fr h ee h rc s I5 l ss ye fr ah h ee h rc .h n e s n e s s ni n b f gf c t b t b ts r a ost o1 BG i n t o aiewt G K i. h smecd n mb rsas n dt d f e t oh ye ae l e t . I5 s o cmp t l i B ,., e a s b h et o e u e i si e o i rn g f e caatr h rc . e F r xmp ,h w r ima e r i eet gwt tee n s n ad: o ea l te ol s d moen rsn i h s ma y t d rs e d t i h a

Sadr tnad
Smlfe ipiid G21 B32 UC2 SUF8 TTaiinl BG rdtoa I5 US2 CUF8 T-

Caatr hrces

Cds oe
BC DB AD 03 58 81 4C C0 E98 EB9 52C 800 A4 BD 9D F3 58 8E 4C A7 E98 EAA 52C 8B7

Notes for Windows' CMD Users: od p yteci s caatr orcl i C T i l h h ee h rc cr t n MD se, o n e t co s te sa n e e y h l y u ed o h oe h l


cr c cd p g , ., 5 0 fr T8 9 6fr B 3 2G K 9 0fr i , 2 1fr C -B , 2 0fr C -L, 3 or t o e a e e . 0 1 o U F , 3 o G 2 1 / B , 5 o B 5 1 0 o U S2 E 1 0 o U S2 E 4 7 e g6 g
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 21/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

fr te oin lD S Y u cn ue cmma d "h p t d p y te cr n cd p g a d cmma d "h p o h ri ga O. o a s o n c c " o i l h ur t o e a e n o sa e n cc c d p g _ u b r t ca g te o e a eY ua oh v t co s a o th t a d p y h caatr (g C ui o e a e n m e "o h n e h cd p g . o l ae o h oe fn ta cn i l te h rc se . o r r s sa e ., e N w C no s r ui C no , O R s ro t e , o sl o L c a o sl N T at fn) a d e e .

5.12 Collating Sequences (for Ranking Characters)


A s i cniso asq e c o caatr i u p ro l e css e ." p l " " O " " a " I srn o tn o s t f e u ne f h rc s n p e r o r ae, ., a p e , B Y , C t . n ot g r rg s e w g i cmp r gs i siw od r h caatr acri t teu d r i cd n mb r (g U - S I caatr y o ai tn , e re te h rc s cod g o h n eln o e u es e . SA CI h rc - n rg f e n yg ., ) eb caatrteod r o teea l w u b " O " " p l " " a "b cueu p r s l tr h v as lr o e h rc ,h re fr h xmp o l e B Y , a p e , C t ea s p ec e e es ae mae cd e e d a t l n mb r h nl ecs l tr T id e n t ge wt tes-ae dictionary order, h r tesmeu p r s a d u e ta o r e e es h o s o a re i h ocld w a t . s h l w ee h a p ec e n a l ecs l tr h v tesmern . n te cmmo po l i od r gs i ss 1 "( n a t ss ree i o r e e es ae h a w a t a k A oh r o n rb m n rei tn i" 0 t ) t i iod rd n e n rg e me f n o " "t " " r t f1 o 9. o H ne i srn o cmp ro o s i sas-ae collating sequence (r e c, ot g r o ai n f tn , ocld n i s rg l o collation)s f nd f e , h hsei s iot en d w i p ci e i c f e tern s o l tr (p ecs, w r s)n mb r a dsei smb l T eeaema y oai sq e cs vib . h a k fre es u p r e l ec e u es n p c ly o . h r r t a o a , , a s n clt g e u ne aaa l ln l e I i e te u t y ut co s aclt gsq e c t mety u a pct n sei rq i me t S mecaset s ni l p o o o h oe oai e u ne o e o r p lai ' p ci e u e ns o ry ln i os f c r . insensitive dictionary-order collating sequences h v tesmern fr a u p r s a dl ecs l tr i. A , ae h a a k o sme p ec e n o r e e es ., ' a w a t ,e ' ' ' ' ' ' ' . ' ' ' ' S mecase-sensitive dictionary-order collating sequences p t h u p r s l tr a B,b . . Z,z.o u te p ec e e e a t b fr i l ecs cu trati. A ' ' ' '. ' ' ' ' ' ' . . y i l, p c i rn e b fr e e t o r e o nep r ., ' B o s w a ,e ' C . a . b c . . Tp ay sae s a kd e e cl o d i ' 't ' ' flw db te l a es it 0 o 9 , l e y h a h b t gs oo p . C lt gsq e c i otnl g a ed p n e ta d f e ta g a e ued f e t eso caatr (g a oai e u ne s f a u g e e d n, s i rn l u g s s i rn st f h rc s e . , , , ) ln e n f e n f e e ., wt te o nod r i h i w res h r .

5.13 For Java Programmers - j v . i . h r e aanoCast


J K1 i rd cdan w j v . i . h r e p ca et sp ot no i /eo i o caatr f m U S2ue D . n o ue 4 t e a a n o c a s t akg o u p r e cd gd cd g f h rc s r n n e o C - sd i en l i Jv po rm t a y u p r dcast sdb etrad v e. n rayn aa rga o n sp ot h r ue y x n l ei s t l e e e c Example: h flwn po rm e cd s o U i d tx i vr u e cd gsh mea dd p y h H x o e o T e oo i rga no e sme n o e et n ai s no i ce ,n i l te e cd s f l g c s o n sa te no e b t sq e cs h e cd d ye e u ne.
ipr jv.i.yeufr mot aanoBtBfe; ipr jv.i.hrufr mot aanoCaBfe; ipr jv.i.hre.hre; mot aanocastCast pbi casTsCastnoeeoe{ ulc ls ethreEcdDcd pbi sai vi mi(tig]ag){ ulc ttc od anSrn[ rs / Tyteecast frecdn / r hs hres o noig Srn[ castae ={U-SI" "S-891,"T-" "T-6, tig] hreNms "SACI, IO85-" UF8, UF1" "T-6E,"T-6E,"B" "I5} UF1B" UF1L" GK, BG"; Srn msae="i !; / msaewt nnACIcaatr tig esg H, " / esg ih o-SI hrces / PitUS2i hxcds / rn C- n e oe Sse.u.rnf"1s " "C-"; ytmotpit(%0: , US2) fr(n i=0 i<msaelnt(;i+ { o it ; esg.egh) +) Sse.u.rnf"0X" (n)esg.hrti) ytmotpit(%4 , itmsaecaA(); } Sse.u.rnl(; ytmotpitn) fr(tigcastae castae){ o Srn hreNm: hreNms / GtaCastisac gvntecastnm srn / e hre ntne ie h hre ae tig Castcast=Castfraecastae; hre hre hre.oNm(hreNm) Sse.u.rnf"1s " castnm() ytmotpit(%0: , hre.ae); / Ecd teUioeUS2caatr it abt sqec i ti cast / noe h ncd C- hrces no ye eune n hs hre. BtBfe b =castecd(esg) yeufr b hre.noemsae; wie(bhseann(){ hl b.aRmiig)
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 22/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Sse.u.rnf"0X" b.e() / Pithxcd ytmotpit(%2 , bgt); / rn e oe } Sse.u.rnl(; ytmotpitn) b.eid) brwn(; } } } US2 04 06 02 6A 57 02 [6btfxdlnt] C-: 08 09 0C 08 9D 01 1-i ie-egh H i , ! U-SI:4 6 2 3 3 2 [-i fxdlnt] SACI 8 9 C F F 1 8bt ie-egh H i , ? ? ! IO85-:4 6 2 3 3 2 [-i fxdlnt] S-891 8 9 C F F 1 8bt ie-egh H i , ? ? ! UF8 4 6 2 E 8 A E A B 2 [- btsvral-egh T-: 8 9 C 6 2 8 5 5 D 1 14 ye aibelnt] H i , ! UF1:F F 0 4 0 6 0 2 6 A 5 7 0 2 [- btsvral-egh T-6 E F 0 8 0 9 0 C 0 8 9 D 0 1 24 ye aibelnt] BM H O i , ! [yeOdrMr idctsBgEda] Bt-re-ak niae i-nin UF1B:0 4 0 6 0 2 6 A 5 7 0 2 [- btsvral-egh T-6E 0 8 0 9 0 C 0 8 9 D 0 1 24 ye aibelnt] H i , ! UF1L:4 0 6 0 2 0 A 6 7 5 2 0 [- btsvral-egh T-6E 8 0 9 0 C 0 8 0 D 9 1 0 24 ye aibelnt] H i , ! GK 4 6 2 C F B C 2 [- btsvral-egh B: 8 9 C 4 A A 3 1 12 ye aibelnt] H i , ! Bg:4 6 2 B 7 A 6 2 [- btsvral-egh i5 8 9 C 1 A 6 E 1 12 ye aibelnt] H i , !

5.14 For Java Programmers - c a and S r n hr tig


T e h rd t tp aeb sdo teoriginal 1 -iU i d s n adcldU S2 T eU i d h s i eeo e t h c a aa y e r ae n h 6b n o e t d r ae C -. h n o e a s c v l d o t c a l c n v 2 b swt cd rn e f + 0 0t U 1 FF. h st f h rc r f m U 0 0 t U FF ik o na teBasic 1 i, i o e a g o U 0 0 o + 0 FF T e e o caat s r t h e o + 0 0 o + FFs n w s h Multilingual Plane ( BMP) h rc r a o e + FF r cldsupplementary caatr A1 -iJv c a cn o h l . aat s b v U FF ae ae C e l h rc s 6b aa h r a n t o e. t d a u p me tr caatr sp l nay h rc . e e R cl h tnteU F1 e cd gsh me aB ea ta i h T-6 no i ce , MPcaatr ue 2b tsI itesmea U S2 Asp l nay l n h rc s ss ye.ts h a e s C -. u p me tr e caatr ss b tsa drq i s p io 1 -ivl stefs f m te i -urg ts a g ,\ D 0 - u B F, h rc ue 4 ye. n e u e a a f 6b a e,h itr h h hsr ae rn e (u 8 0 \ D F ) e r r t u r o g o te eo df m teo -urg ts a g (u C 0 \ D F ) h scn r h l sr ae rn e \ D 0 - u F F. o w o I Jv, S r n iasq e cs f n o ecaatr Jv, f tue U F1 fr t i ga dS r n B f e . o n aa a t i g s e u ne o U i d h rc s aa i a , ss T-6 o S r n n t i g u f r F r c e. n c B MPcaatr te aetesmea U S2 F rsp l naycaatr ec caatr rq i sap i o c a h rc s h y r h a e, s C -. o u p me tr h rc s ah h rc s e u e e e, e r a f hr r vl s a e. u Jv meh d ta acp a 6b c a vl d e n t u p r sp l nay h rc r Meh d ta acp a 2b aa to s h t ce t 1 -i h r a e o s o sp ot u p me tr caat s to s h t ce t 3 -i t u e e. t i tvl sp ot l n o e h rc r ( teo e 2 b s i l i sp l nay h rc r n a e u p ra U i d caat si h l r 1 i)n u n u p me tr caat s u l c e n w t, c d g e e. T iimen t b a aa e c i us n I ae e t e cu trh ue fu p me tr caatr h s a to e n cd mid csi . h v yto no ne te s o sp l nay h rc s s s o e e!

5.15 Displaying Hex Values & Hex Editors


A t sy uma n e t d p yte e vl s f f , seili d an wt U i d caatr AHex Editor ia t i ,o me y ed o i l h h x a e o a i ep c l n elg i n o e h rc s sa u l e ay i h c e. s h n yto ta ag o po rmme so l p sesnh /e to o. h r aema yf e aesae aeH x d o a d o lh t o d rga r h u oss i ih r o l xT ee r d s b n r w r/ rw r e E i r e h t aaa l Ty o g " e E i r vib .r g o l H x d o" l e e t . I sdte oo i s ue h flwn : l g N tP d + wt H xE i r l - : p nsuc a df e Y ucntg l b t enH x i a dN r l i b oe a + i e d o P gi O e -o r n r . o a o g ew e e v w n oma v w y h t u n e e e e e p si te H b t n uh g h " " ut . n o P P d Few r. o cntg lt H x i b co s g"i " n a dsl tH x d Mo e. S a :re aeY u a o g o e v w y h oi Ve me u n e c " e E i d " e e n w e t
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 23/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

Tx a :h rw r wto t xi t np r d T v wteH x a e y un e t "p n te i b co s gte i et d S ae ae i u ep ai ei .o i h e vl , o ed o o e " h f y h oi h f P h r o o e u l e n l e fr t fb ay ( ) oma o "i r"? . n ? Ut E i S ae aen tre3 -a tao l lad :h rw r, o f , 0d y r l n . r t e i y Lt k o i o h v ab t r h i , h hif tol nh es t ue cntg l b t enH x n n r l i , e me n w f u ae et co e w i s a t a c, ay o s,a o g ew e e a d oma v w y e c c s u e e f e. r ,. e . . T e oo i Jv po rm cnb ue t d p y e cd fraa r t e ( tg rcaatr n f ai -o t h flwn aa rga a e sd o i l h x o e o Jv Pi i si e e,h rc a d l t gp i ) l g sa mi v n e o n n:
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 pbi casPiteCd { ulc ls rnHxoe pbi sai vi mi(tig]ag){ ulc ttc od anSrn[ rs iti=135 n 24; Sse.u.rnl(Dcmli "+i; ytmotpitn"eia s ) / 135 / 24 Sse.u.rnl(Hxi "+ItgrtHxtigi) ytmotpitn"e s nee.oeSrn(); / 33 / 09 Sse.u.rnl(Bnr i "+ItgrtBnrSrn();/ 1000110 ytmotpitn"iay s nee.oiaytigi) / 1000101 Sse.u.rnl(Otli "+ItgrtOtltigi) ytmotpitn"ca s nee.ocaSrn(); / 301 / 07 Sse.u.rnf"e i %\" i; ytmotpit(Hx s xn, ) / 33 / 09 Sse.u.rnf"ca i %\" i; / 301 ytmotpit(Otl s on, ) / 07 ca c='' hr a; Sse.u.rnl(Caatri "+c; ytmotpitn"hrce s ) / a / Sse.u.rnf"hrce i %\" c; ytmotpit(Caatr s cn, ) / a / Sse.u.rnf"e i %\" (hr)) ytmotpit(Hx s xn, sotc; / 6 / 1 Sse.u.rnf"eia i %\" (hr)) / 9 ytmotpit(Dcml s dn, sotc; / 7 fotf=35; la .f Sse.u.rnl(Dcmli "+f; ytmotpitn"eia s ) / 35 / . Sse.u.rnl(la.oeSrn();/ 01c1(rcin1c Epnn=) ytmotpitnFottHxtigf) / x.p Fato=., xoet1 f=-.5; 07f Sse.u.rnl(Dcmli "+f; ytmotpitn"eia s ) / -.5 / 07 Sse.u.rnl(la.oeSrn();/ -x.p1(=18 E-) ytmotpitnFottHxtigf) / 018- F-., =1 dul d=1.2 obe 12; Sse.u.rnl(Dcmli "+d; ytmotpitn"eia s ) / 1.2 / 12 Sse.u.rnl(obetHxtigd) / 016037ad13(=.7ad037 E3 ytmotpitnDul.oeSrn(); / x.7ad037p F16037ad1 =) } }

I Els, o cnv wteh x o efr n cpe y u a i h e cd o integer pi t eJv vr b snd b gmo e s oo sI d b gp r et e i e r i aa ai l i e u mi v ae d a flw : e u es cv, l n p i "ai l p n l Sl t h " n " i etd ta g ) Jv Jv Pe rne. Pi t eDsl O t n V r b " a e e c te me u ( vr ae e n e r n l aa i e aa rf e cs. e . r i i a pi s mi v py o C ek Dsl h xd c la e (yesotca, tl g" h c " i a ea ei vl sb t,h r h ri , n ) py ma u , n o .

6. Summary - Why Bother about Data Representation?


Itg rn mb r1 f ai -o tn mb r1 0 caatrsmb l 1 , n s i " " aettl d f e ti i te ne e u e ,l t gp i u e . h rc o n n e y o ' ' a d tn 1 r oay i rn n d h rg l f e se cmp tr moyY un e t k o te i rne ow i g o a dh hp r r ne rga . o ue me r. o ed o n w h d f e c t re o d n i -ef ma c po rms f e t g o I 8b signed integer, tg r u e 1irpee tda 0 0 0 0 B n -i t i e e n mb r s e rsne s 0 0 0 1 . n I 8b unsigned integer, tg r u e 1irpee tda 0 0 0 0 B n -i t i e e n mb r s e rsne s 0 0 0 1 . n I 1 -isigned integer, tg r u e 1irpee tda 0 0 0 0 0 0 0 0 B n 6b t i e e n mb r s e rsne s 0 0 0 0 0 0 0 1 . n I 3 -isigned integer, tg r u e 1irpee tda 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B n 2b t i e e n mb r s e rsne s 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 . n I 3 -i floating-point representation,n mb r1 0 i rpee td a 0 0 1 1 1 0 0 0 0 0 0 0 0 n 2b t u e . s e rsne s 1111 000 0000 0 0 0 0 B i. = , = 2 , = . 0 0 0 0 ,e S 0 E 1 7 F 0 ., I 6 -i floating-point representation,n mb r1 0 i rpee td a 0 0 1 1 1 1 1 0 0 0 0 0 0 n 4b t u e . s e rsne s 11111 00 0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B i. = , = 0 3 F 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,e S 0 E 1 2 , = . .,
www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html 24/25

4/7/12

A Tutorial on Data Representation - Integers, Floating-point numbers, and characters

I 8b Lt -,h caatr y o ' 'irpee tda 0 1 0 0 B(r 1 ) n -i ai 1 te h rc smb l 1 s e rsne s 0 1 0 1 o 3 H. t n e I 1 -iU S2 te h rc r y o ' 'irpee tda 0 0 0 0 0 1 0 0 B n 6b C -,h caat smb l 1 s e rsne s 0 0 0 0 0 1 0 1 . t e I U F8 te h rc r y o ' 'irpee tda 0 1 0 0 B n T-,h caat smb l 1 s e rsne s 0 1 0 1 . e Iy u"d " 1 -is n di e e 1a dLt - caatr 1 o a tn " " y ucu g t srre f o a d a 6b i e n g r n ai 1 h rc ' ' r s i 1 , o o l e a upi . tg t n e rg d s

6.1 Exercises (Data Representation)


F rh flwn 1 -icd s o te oo i 6b o e: l g t
00 00 01 11; 00 00 00 00 10 00 01 11; 00 00 00 00

Gv te vl si h y r rpee t g i h i a e, te ae e rsni : e r u f n 1 a 6b u s n di e e; . 1 -i ni e n g r t g t 2 a 6b s n di e e; . 1 -i i e n g r tg t 3 t o8b u s n di e es . w -i ni e n g r t g t ; 4 t o8b s n di e es . w -i i e n g r tg t ; 5 a 6b U i d caatr . 1 -i n o e h rc s t c e; 6 t o8b I -8 91caatr . w -i S 8 5 - h rc s t O e. A s() 2 3 8 0 () 2 - 2 2 ; ) , 2 1 8 4 ; ) , 2 - 2 , 2 () * ; ' () U , * ; A , * . n: 4 , 2 1 ; 4 , 3 7 6 ( 0 4 ; 2 , 2 ( 0 4 ; 1 8 4 ; ' ' ' ; N L ' ' P D ' ' 1 2 3 4 5 6

REFERENCES & RESOURCES


1 (o t gP i N mb r p ci t n I E 5 (9 5,I E tn adfr i r F ai -o t rh t" . F ai -o t u e S ei ai )E 7 4 1 8 ) E Sa d r o B ay l t gP i Ai mei. l n n f o c E "E n o n n t c 2 (S IS ei ai )S / C6 6(9 1 (rT - T 01 9 )" fr t ntcn l y- -icd dcaatr e . A CI p ci t n I I 4 1 9 )o I T . -9 2,I omai eh o g 7b o e h rc st f o c O E U 5 n o o t e frnomai i eca g " o i r t n n r n e. f o t h 3 ( t - S ei ai )S / C 8 5 -,Ifr t ntcn l y- -is g -yecd dga h caatr es. L i I p ci t n I I 8 91 " omai eh o g 8b i l b t o e rp i h rc st an f o c O E n o o t ne c e P r1 Lt a h b t o 1. at :ai l a e N . " n p 4 (n o eS ei ai ) S / C 1 6 6 " fr t ntcn l y-U i r l l l O tt o e C aatr e . U i d p ci t n I I 0 4 , I omai eh o g c f o c O E n o o n es Mu ie c C d d h rc St v a t - e p e (C ) U S. " 5 U i d C not m @ ht:w wu i d . g . n o e o sru c i t / w . c e r. p/ no o

Last modified: December, 2011

Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan (ehchua@ntu.edu.sg) | HOME

www3.ntu.edu.sg/home/ehchua/programming/java/DataRepresentation.html

25/25

You might also like