Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Bool

ean I
nteger
s Shis
bool
:Thepos
sibl
eval
uesar
econs
tant
str
ueandf
als
e. int/
Uint:Signedandunsi
gnedintegersofvarioussiz
es. Theres
ultofashi oper
a on
Keywordsui
nt8touint
256instepsof8 hast
het ypeoft
hele operand,t
runc
a ngt
her
esul
ttomat
ch
Operators : (uns
ignedof8upto256bits)andint8toint256.uintandint thet
ype.
!(l
ogi calnega on) areali
asesf
oruint
256andint256,res
pec vely.
&& (logicalconjunc on,“and”) F
orpos iveandnega vexval ues,x<<yi sequi
valenttox*2**y .
||( logicaldis
junc on,“or”) Oper at
ors: F
orpos ivexvalues,x>>yi sequivalentt
ox/2**y .
==( equal i
ty) Compar is
ons:<=,<,==,!=,>=,>(evaluatetobool) F
ornega vexvalues,x>>yi sequi val
entto(x+1)/2**y-1
!=(inequal i
ty) Bitoperators:&,|,^(bit
wi s
eex c
lusiveor)
,~(bit
wisenega on) (
whic
hi sthesameasdi vi
dingxby2**ywhi l
eroundingdown
Shi oper at
or s
:<<(l
e s hi) ,>>(ri
ghtshi) t
owardsnega veinfinit
y).
Theoper at
ors||and&& appl ythecommons hort-
cir
cuing Arithme coper at
ors:+,-,unary-,*,/,% (
modulo),** I
nal
lc ases
,shiingbyanega veyt hrowsarun meex cep on.
rul
es.Thismeansthatintheexpr es
sionf
(x)||g(y), (exponen a on)
iff
(x)evaluat
estotr
ue,g( y)wil
lnotbeevaluat
ed
evenifitmayhaveside-effects. Addr
ess
Modul
o Theaddr esst ypec omesi ntwoflavour s,whic
har el argel
yiden c al
:
addres s:Holdsa20byt eval ue( s
izeofanEther eum address).
Themodul ooper a ona% nyi eldst her emai nderr
Exponen a on a erthedivi
s ionoftheoper andabyt heoper andn,wher
eq=int(
a/n)
addres spayabl e:Sameasaddr es
s,butwitht headdional
member str
ans ferands end.
Exponen a onisonl yavail
ablef
orunsignedtypes
. andr=a-( n*q) .Thismeanst hatmodul oresult
sint
hesames
ignasi
ts
Theideabehi ndthi sdis nc oni sthataddresspayabl eisan
Pleasetakecar
et hatthetypesyouareus i
ngare le operand(orz ero)anda% n==- (-
a% n)
addressyouc ansendEt herto,whi leaplai
naddr essc annotbes ent
largeenoughtohol dtheresul
tandprepare hol
dsfornega vea:
Ether
.
forpoten alwrappingbehaviour
. •int256(5)% int256(2)==i nt256( 1)
Typeconver sions:
•int256(5)% int256(-2)==i nt256( 1)
Impli
citconver si
onsf r
om addr esspayabletoaddr essar eal
lowed,
•int256(-5)% int256(2)==i nt256( -1)
Di
vis
ion •int256(-5)% int256(-2)==i nt256( -1)
whereasc onver s
ionsf rom addr esstoaddresspayabl earenot
possi
ble( t
heonl ywayt operfor ms uchaconvers
ioni sbyus ing
Sincethet ypeoft heresultofanoper a oni salwaysthe anintermedi atec onversiontoui nt160).
typeofoneoft heoper ands ,divi
siononi ntegers
alwaysr es
ul t
sinani nteger.InSolidity,divi
sionroundst
owar
dsz
ero.
Fi
xedSi
zeByt
eAr
rays Addressliteralscanbei mpli
c i
tlyconvertedtoaddr esspayable.

Thismeant hatint256(-
5)/i nt256(2)==i nt256(-
2). Thevaluet ypesbytes1,bytes 2,bytes3,…,bytes32holdasequenc
e Explici
tc onver sionst oandf rom addressar eall
owedf ori nteger
s,
Notet hatinc ontr
ast,divi
siononl iteralsresult
sin ofbytesfr om onet oupt o32.byt eisanaliasforbytes
1. integerliter
al s,byt es20andc ontracttypeswi ththefol l
owi ng
frac onalvaluesofar bit
rarypr eci
s i
on. Operators: caveat:
Compar i
sons :<=,<,==,!=,>=,>( evaluatetobool) Conver si
onsoft hef orm addresspayable(x)arenotal l
owed.
Bitoperator s:&,|,^( bit
wiseex clusiveor)
,~( bi
twis
enega on) Insteadt her esul tofac onversi
onoft hef orm address(x)has
Addion,Subt
rac onMulpl
ica on Shi oper at ors:<<(l
e s hi) ,>>( ri
ghtshi) thet ypeaddr esspayabl e,ifxisofintegerorfix edbytest ype,a
Indexacces s :I
fxisoft ypebyt esI,thenx[k]for0<=k<Ir etur
nsthekt
h lit
eralorac ont rac twit
hapayabl efal
lbac kfunc on.Ifx
Addion,s ubtrac onandmulpl i
ca onhavetheusualseman cs
.
byte(read- only). isac ontractwi thoutpayabl efall
backf unc on,
Theywrapi ntwo’ scomplementrepres
enta onmeaningthatf
orexampl
e
uint
256(
0)-ui nt256(1)==2**256-1. thenaddr es
s (x)wi l
lbeoft ypeaddres s
.I nexternalf
unc on
Theshiingoper
atorworkswi
thanyintegert
ypeasrightoper
and signaturesaddr es sisusedforbot htheaddr es
sandt he
Youhavetot aket heseoverflowsint
oac c
ount
(butr
etur
nsthetypeoft
hele operand),whi
chdenotesthenumber addr esspayabl et ype.
whendesignings afesmartcontr
acts
.
ofbit
stoshi by
.Shiingbyanega veamountc aus
esar un meexc
ep on
Theexpression-xisequival
entto(T(0)-x)whereTis Operator
s:
Members
: <=,<,==,!
=,>=and>
thetypeofx.
.l
engt
hyiel
dst
hefix
edl
engt
hoft
hebyt
ear
ray(
read-
onl
y).
Thismeanst hat-xwillnotbenega veifthetypeofx
isanunsignedintegertype.
Member
sOfAddr
ess
Also,-
xcanbepos ivei fxisnega ve. Dynami
cal
lySi
zedByt
eAr
ray
Foraqui ckr eferenceofallmember sofaddr es
s,see
bytes
: Member sofAddr es
sTypes .
Therei
sanothercaveatal
so
Dynami
cal
ly-
siz
edbyt
ear
ray
,seeAr
rays
.Notaval
ue-
type! balanceandt ransfer
r
esulti
ngfrom t
wo’scomplement
iti
spos sibletoquer ythebal anc
eofanaddr essusi
ngthepropert
y
s
tring: balanceandt os endEther(inunitsofwei )t
oapayableaddress
r
epr esentati
on: Dynami
cal
ly-
siz
edUTF-
8-enc
odeds
tri
ng,s
eeAr
rays
. usi
ngt het ransferfunc on:
i
ntx=- 2**255; Notaval
ue-t
ype! addr esspayablex=addr ess
(0x123);
addr essmyAddr es
s=addr ess(t
his)
;
assert(-
x==x) ;
if(x.balance<10&& myAddr ess.
balance>=10)x.
trans
fer(
10);

Thetransferf
unc onfail
sifthebalanceofthec urr
entc
ont
ractis
notlargeenoughoriftheEthertransf
erisreject
edbyther
eceivi
ng
ht
tps:
//vomt
om.
at acc
ount .Thetrans
ferf
unc onr evert
sonf ai
lure.

You might also like