SetFormat, Integer, Unsigned
Posted: 31 Dec 2017, 16:48
It is a pain trying to do bitwise operations with all 64 bits of an integer. It is especially weird when the numbers are negative, where bits flip, adding becomes subtracting, etc (more examples and current workarounds below). Keeping everything positive would hopefully fix this. I just want a way to store and manipulate bits.
My wish also includes being able to choose and stay with integer sizes (32 or 64 bits), but since that is slightly different, I'll put it in another post.
*note: I am aware that format("{:u}", num) displays numbers as if they were unsigned, but it does nothing to solve the screwyness when operations are applied.
Thanks AutoHotkey gods
Code: Select all
msgbox, % (2 ** 63 == 2 ** 64) ;2 ** 64 should be 0
;workaround: just don't do it
x := 2 ** 63 ;the negative bit
msgbox, % (x == 9223372036854775808 OR x == 0x8000000000000000) ; 2 ** 63 = bin:1000000000..., but 9223372.. and 0x8000.. round down to bin:011111111...
;workaround:
; msgbox, % (x == -9223372036854775808 OR x == -0x8000000000000000)
x := 2 ** 63
msgbox, % (x >> 1 == 2 ** 62) ;when shifting bits right, the negative bit doesn't move (e.g. 1000000.. >> 1 = 1100000..) shifting left works just fine, though
;workaround:
; x >>= 1
; if(x < 0)
; x ^= 2 ** 63
; msgbox, % (x == 2 ** 62)
;EDIT: additional point: getting the last n bits of a number
x := 2 ** 63 | 2 ** 6 | 2 ** 5 | 2 ** 0
msgbox, % Mod(x, 2 ** 6) ;100...001100001 makes all 0's between the first bit and 1100001 1's
;workarounds:
; Mod(x & 0x7FFFFFFFFFFFFFFF, 2 ** 6) ;remove negative bit, assumes at least 1 bit is being removed
; or
; toBin(x & 0x3F) ;bitwise AND with a number with n 1's where n is the amount of digits to keep, not very intuitive
*note: I am aware that format("{:u}", num) displays numbers as if they were unsigned, but it does nothing to solve the screwyness when operations are applied.
Thanks AutoHotkey gods