You are on page 1of 1

SOLI

DITYTYPESCHEATSHEET
BOOLEANS I
NTEGERS SHI
FTS
bool:Thepossi blevaluesareconstant
str
ueandf
alse. int/uint :Signedandunsignedi nt
eger
sofvarioussizes. Theresul
tofashif
toperati
on
Oper ators: Keywor dsuint8touint256instepsof8 hast
het ypeoft
hel ef
toperand,t
runcat
ingt
her
esul
ttomat
cht
het
ype.
!(logicalnegat ion) (unsi
gnedof8upt o256bits)andint8t
oint256.
&&( l
ogicalconj uncti
on,“and”) uintandintar eali
asesforuint256andint
256,respectivel
y. Forpositiveandnegat i
vexval ues,x<<yi sequivalentt
ox*2**y.
||(logicaldisjuncti
on,“or”) Forpositivexval ues,x>>yi sequivalenttox/2**y.
==( equality) Oper at
or s: Fornegat ivexval ues,x>>yi sequivalentto(x+1)/2**y-1
!=(inequal i
ty) Compar isons:<=,<,==,! =,>=,>(evaluatetobool) (whi
chi st hesameasdi vi
dingxby2**ywhi l
er oundingdowntowar
ds
Bitoper ators:&,|,^( bi
twiseexclusi
veor )
,~(bi
twisenegat
ion) negativeinfinity).
Theoper at
ors||and&&appl ythecommonshor t-ci
rcui
ting Shiftoper ators:<<(leftshif
t),>>(ri
ghtshift
) Inallcases,shif
tingbyanegat iveythr owsaruntimeexcepti
on.
rul
es.Thismeanst hatintheexpr essi
onf(x)||g(y), Arithmet i
coper at
ors:+,-,unary-,*,/,% (
modulo),**
iff(
x)evaluat
est otrue,g(y)willnotbeevaluated (exponent iati
on) ADDRESS
evenifitmayhavesi de-effects.
Theaddresstypecomesi nt
woflavour s,whi
charel ar
gelyidentical:
MODULO address:Holdsa20bytevalue(sizeofanEt hereum address).
EXPONENTI
ATI
ON Themodul ooper ationa% nyi eldstheremainderr addresspayable:Sameasaddress,butwi t
ht headditi
onalmember s
Exponentiationisonlyavail
ableforunsignedtypes. afterthedi visionoft heoper andabyt heoperandn, transf
erandsend.
Pleasetakecar ethatthetypesyouareusi ngare wher eq=i nt (a/n)andr=a-( n*q) .Thismeansthatmodulo
largeenought ohol dtheresul
tandpr epare resultsinthesame Theideabehindthi
sdist
inct
ionisthataddresspayabl
eisanaddr
essyou
forpotentialwrappingbehaviour
. signasi tsleftoper and( orzer o)anda% n==- (-a% n) cansendEtherto,whi
leaplainaddresscannotbesentEther
.
holdsf ornegat ivea:
DI
VISI
ON •int256(5)% i nt256( 2)==int256(1) •int
256(5)% Typeconver si
ons:
int256(-2)==i nt256( 1) Impli
citconver si
onsfrom addresspayabl
etoaddressar
eallowed,whereas
Sincet hetypeoft heresultofanoper ation •int256(-5)% i nt256( 2)==int256(-
1) •int256(
-5)% conversi
onsf rom addresstoaddresspayablear
enotpossi
ble(theonlyway
isalwayst het ypeofoneoft heoper ands, int256(-2)==i nt256( -1) toperform suchaconver sionisbyusingan
divisi
ononi nteger sal
waysresultsinani nteger
. int
ermedi at
econver si
ont ouint160).
InSolidity,divisi
onr oundstowardszer o.
Thismeant hati nt256(-
5)/int256(2)==int256(-2)
. FI
XED-
SIZEBYTEARRAYS Addr
essl
iter
alscanbei
mpl
ici
tlyconver
tedt
oaddr
esspayabl
e.
Thevaluetypesbytes1,bytes2,bytes3,…,bytes32hol
dasequenceofbytes
Notethati
ncontrast
,divisi
ononl
iter
alsr
esul
tsi
nfr
act
ional from onetoupto32.bytei sanaliasforbytes1. Explici
tconver sionstoandf rom addr essar eall
owedf ori ntegers,integer
val
uesofarbi
trar
ypr eci
sion. li
terals,byt es20andcont racttypeswi tht hef ol
lowingcaveat :
Operators:
Conver si
onsoft hef orm addr esspayabl e(x)arenotal lowed.Inst eadthe
Comparisons:<=,<,==,!
=,>=,>( eval
uatetobool)
ADDI
TION,SUBTRACTI
ON MUL
TIPLCATI
ON resultofaconver si
onoft hef or m addr ess(x)hast hetypeaddr esspayable,

Addit i
on,subt ractionandmul ti
plicat i
onhave
Bi
toperators:&,|
,^(
bitwi
seexcl
usi
veor)
, (
bi~
twi
senegati
on) ifxisofi ntegerorfixedbyt est ype,al iteraloracont ractwi t
hapayabl e
fall
backf unct ion.Ifxisacont ractwit houtpayabl efallbackfunct i
on,
theusualsemant i
cs. thenaddr ess(x)wi l
lbeoft ypeaddr ess.I next ernalfunct i
onsignat ur
es
Theywr api nt wo’scompl ementr epr esent
ation Shi
ftoperators:<<(
leftshi
ft)
,>>(
rightshi
ft)
addressi susedf orbotht headdr essandt headdr esspayabl et ype.
meani
uint
ngt
256(
hatf
0)-ui
orexampl
nt256(
e
1)==2**256-1. I
ndexaccess:I
fxi I
softypebytes ,thenx[ 0 I
k]for <=k< returnsthekthbyte Oper
ators:
Youhavet ot aket heseover flowsi nt oaccount (
read-
only). <=,<,==,!=,>=and>
whendesi gningsaf esmar tcontract s.
Theexpr ession- xisequi valentto( T(0)-x)wher eTis Theshiftingoperatorworkswithanyintegertypeasri
ghtoperand MEMBERSOFADDRESS
thet ypeofx. (butreturnsthetypeoftheleftoperand),
Thismeanst hat- xwi l
lnotbenegat iveifthetypeofx Foraqui ckrefer
enceofallmember sofaddr
ess,see
whichdenotesthenumberofbi tstoshi
ftby.Shifti
ngbyanegativeamountcauses Member sofAddressTypes.
isanunsi gnedi ntegert ype. aruntimeexception.
Also,-xcanbeposi ti
vei fxisnegat ive. balanceandt r
ansfer
itispossibl
etoquer ythebalanceofanaddressusi
ngtheproper
ty
Therei sanothercaveatal
so Members: balanceandt osendEther(inunitsofwei
)toapayableaddr
essusingt
he
result
ingf r
om two’scomplement .l
engthyiel
dsthefixedl
engthofthebytearray(
read-
onl
y). transferfunct
ion:
representation:
intx=- 2**255; addresspayabl
ex=addr ess(
0x123);
assert(
-x==x) ;
DYNAMI
CALL
Y-SI
ZED BYTEARRAY addressmyAddress=address(t
his);
bytes: if(
x.balance<10&&myAddr ess.balance>=10)x.
transf
er(
10)
;
Dynami
cal
ly-
sizedbyt
ear
ray,seeAr
rays.Notaval
ue-
type!
Thetransf
erfunct
ionfail
sifthebalanceofthecurr
entcontracti
snotl
arge
st
ring: enoughorift
heEt hertr
ansferisrej
ectedbytherecei
vingaccount.
Dynami
cal
ly-
sizedUTF-
8-encodedst
ring,seeAr
rays.Notaval
ue-
type! Thetransf
erfunct
ionrevert
sonf ail
ure.

ht
tps:
//vomt
om.
at

You might also like