I have an issue with bitwise shifting. It works for 32 bit, but I need this up to a 64-bit number.
Example:
select
(1 << 1) as ok_1,
(1 << 2) as ok_2,
--...
(1 << 30) as ok_30,
(1 << 31) as NOT_ok_31
In the last column (NOT_ok_31) instead -2147483648 I expect (10000000000000000000000000000000)₂ = (1 × 2³¹) ... (0 × 2¹) (0 × 2⁰) = 2147483648
Any Idea? Thanks in advance.
CodePudding user response:
32-bit integer (10000000000000000000000000000000)₂ has the sign bit set so it is a negative number. First cast 1 as binint
so that the result will be a 64-bit integer too.
select
(1 << 1) as ok_1,
(1 << 2) as ok_2,
(1::bigint << 31) as ok_31,
(1::bigint << 33) as ok_33;
ok_1 | ok_2 | ok_31 | ok_33 |
---|---|---|---|
2 | 4 | 2147483648 | 8589934592 |