-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearningConversions
More file actions
27 lines (21 loc) · 827 Bytes
/
learningConversions
File metadata and controls
27 lines (21 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pragma solidity >= 0.8.20
contract learnConversions {
// uint (default alias for uint256) is a unsigned integer, which has:
// - minimum value of 0
// - maximum value of 2^16-1 = (115792089237316195423570985008687907853269984665640564039457584007913129639935)
//78 decimal digits // uint number = 2;
// Conversion to smaller type costs higher order bits
uint32 a = 0x12345678;
uint16 b = uint16(a); // b = 0x5678
// Conversion to higher type adds padding bits to the left
uint16 c = 0x1234;
uint32 d = uint32(c); // d = 0x00001234
// What is the cost?
// Conversion to smaller bytes costs higher order data
bytes2 e = 0x1234;
bytes1 f = bytes1(e); // f = 0x12
// What does this add?
// Conversion to larger bytes adds padding bits to the right
bytes2 g = 0x1234;
bytes4 h = bytes4(g); // h = 0x12340000
}