Skip to content

Instantly share code, notes, and snippets.

@Rami-Majdoub
Created December 31, 2022 01:33
Show Gist options
  • Save Rami-Majdoub/ac542597e289e32e678f5598247bfd9f to your computer and use it in GitHub Desktop.
Save Rami-Majdoub/ac542597e289e32e678f5598247bfd9f to your computer and use it in GitHub Desktop.
//////////// uint
uint8(x) , x £ [0, 255] = [0x00, 0xff] = [0, 2^8 - 1]
uint16(x) , x £ [0, 65535] = [0, 2^16 - 1]
uint24(x) , x £ [0, 16777215] = [0, 2^24 - 1]
uint32(x)
uint40(x)
...
uint256(x) , x £ [0, 2^256 - 1]
//////////// int
int8(x) , x £ [-128, 127] = [-2**(8-1) , 2**(8-1)-1]
//////////// bytes
uint8(x) = bytes1(x) , x £ [0x00, 0xff]
uint16(x) = bytes2(x) , x £ [0x0000, 0xffff]
hex"Ff" = 0xff
bytes1(x) , x £ [0x00, 0xff]
bytes2(x) , x £ [0x0000, 0xffff]
bytes3(x) , x £ [0x000000, 0xffffff]
...
bytes32(x)
bytes != bytes1 (sol v < v0.8.0 , bytes == bytes1)
0xff_ff is int_const
hex"ff_ff" is bytes
///// bytes shift (NO overflow error)
» bytes2(hex"ffff") << 1
0xfffe
» bytes2(hex"ffff") << 2
0xfffc
» bytes2(hex"ffff") << 8
0xff00
» bytes2(hex"ffff") << 16
0x0000
» bytes2(hex"ffff") >> 16
0x0000
» bytes2(hex"ffff") >> 8
0x00ff
//////
» bytes2(0x1122)
0x1122
» bytes2(0x1122)[0]
0x11
» bytes2(0x1122)[1]
0x22
» bytes2(a[0]) >> 8 | bytes2(a[1])
0x2211
» bytes3 b = bytes3(0x112233)
0x112233
» bytes3(b[0]) >> 16 | bytes3(b[1]) >> 8 | bytes3(b[2])
0x332211
» bytes3(b[1])
0x220000
» b.length
3
//// int bytes shift
9 << 2 = 36 , x * 2**y
9 >> 2 = 2 , x / 2**y
-3 << 1 = -6
-5 >> 1 = -3 (E(-2.5))
//////// bitwise negation (be careful)
~int256(0) == int256(-1)
» ~0x01
-2
» // 0000 0001 => 1000 0010 !!
» ~0x00
-1
» // 0000 0000 => 1000 0001 !!
///////////// Two's Complement
// representing -1
0000 0001
1. invert the digits
1111 1110
2. add 1
1111 1111
// get -1 from binary representation
invert digits
0000 0000
add 1
0000 0001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment