What are registers and what shift bits does?

According to documentation shift left 1 << 62, what does this mean though? I’m not too sure how registers work in Sway.

#[test]
 fn foo() {
     let current_timestamp = timestamp();
     log(current_timestamp);
     let calculated = timestamp() - (10 + (1 << 62));
     log(calculated);
     let sixtwo = (1 << 62);
     log(sixtwo);
 }

Output:

Decoded log value: 4611686018427387924, log rb: 1515152261580153489
Decoded log value: 10, log rb: 1515152261580153489
Decoded log value: 4611686018427387904, log rb: 1515152261580153489

Hey @eesheng, the << operator in sway is used for bitwise left shift operations. The expression 1 << 62 means shifting the binary representation of the number 1 to the left by 62 positions. This operation effectively multiplies the number by (2^{62}).

In your code,

timestamp value is 4611686018427387924 thats logged. 1 << 62 results in 4611686018427387904. Adding 10 to 4611686018427387904 gives 4611686018427387914. Subtracting this from the current timestamp results in 10. thus the value 10 is logged.

This code directly calculates 1 << 62 ie 4611686018427387904.
and thus the value 4611686018427387904 is logged.

1 Like

What do you mean by logged?

Ohh by logged i meant its printed in the terminal

1 Like

Why didn’t 1 << 62 overflow? For Rust, it does overflow and panic reverts.

In Rust 1 << 62 overflows because 1 without type annotation is by default i32 . It is essentially the same as having 1_i32 << 62_i32. In Sway, the default numeric type is u64 which means the expression gets interpreted as 1u64 << 62u64 which properly gives 2^62 = 4611686018427387904. Of course, if you write in Rust 1u64 << 62 it will also not overflow and will result in the same number.