SetFormat, Integer, Unsigned Topic is solved

Propose new features and changes
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

SetFormat, Integer, Unsigned

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.

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
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 :)
Last edited by buttshark on 01 Jan 2018, 04:22, edited 2 times in total.
I have no idea what I'm doing.
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: SetFormat, Integer, Unsigned

31 Dec 2017, 18:11

It was brought to my attention that bitwise shifting left is supposed to copy the 1st bit, that's my bad for not knowing the standard. Just ignore the 3rd example.

I also want to mention that these workarounds in it of themselves is not a big deal. The main reason why I want the ability to format integers as unsigned is that I print the numbers in binary while debugging, and dealing with negative numbers is no fun. My function, if found useful:

Code: Select all

toBin(num)
{
	ret := ""
	while(num)
	{
		ret := (Mod(num, 2) ? "1" : "0") . ret
		num /= 2
	}
	return ret
}
I have no idea what I'm doing.
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: SetFormat, Integer, Unsigned

31 Dec 2017, 18:37

Alas, it wasn't that bad to fix.

Code: Select all

toBin(num)
{
	ret := ""
	loop 64
		ret .= num & 2 ** (64 - A_Index) ? "1" : "0"
	return SubStr(ret, InStr(ret, "1"))
}
Do what you wish with my post. It's no longer necessary.
I have no idea what I'm doing.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SetFormat, Integer, Unsigned

31 Dec 2017, 19:59

- Thanks for this interesting post (and the other one which I have linked to below).
- Btw I'm not sure if var & (2**63) is reliable when doing hex to bin.
- Also, I'm not exactly sure why the & operations below give different results.
- In addition, you might find some of the info, in the Wish List 2.0 link, interesting.

Code: Select all

q:: ;bitwise AND applied to 0x8000000000000000
vNum := 0xFFFFFFFFFFFFFFFF
MsgBox, % !!(vNum & 0x8000000000000000) ;1
MsgBox, % !!(vNum & (2**63)) ;0

MsgBox, % (0x8000000000000000 = 2**63) ;0
MsgBox, % (0x4000000000000000 = 2**62) ;1

vNum1 := 0x8000000000000000
vNum2 := 2**63
MsgBox, % vNum1 "`r`n" vNum2 "`r`n`r`n" (vNum1+0) "`r`n" (vNum2+0)

vNum1 := 0x4000000000000000
vNum2 := 2**62
MsgBox, % vNum1 "`r`n" vNum2 "`r`n`r`n" (vNum1+0) "`r`n" (vNum2+0)
return

MsgBox, % (0xF000000000000000 = 2**64)
MsgBox, % (0x10000000000000000 = 2**65)
return

;==================================================

w:: ;display UInt64 as hex/bin
vNum := 0xFFFFFFFFFFFFFFFF
;MsgBox, % vNum & 0x8000000000000000
MsgBox, % !!(vNum & 0x8000000000000000) ;1
MsgBox, % !!(vNum & (2**63)) ;0
;displays number incorrectly:
MsgBox, % vNum "`r`n" Format("0x{:X}", vNum) "`r`n" Format("{:u}", vNum)
;displays number correctly:
MsgBox, % vNum "`r`n" Format("0x{:X}", vNum << 1 >> 1) "`r`n" Format("{:u}", vNum << 1 >> 1)

oArray := {0:"0000",1:"0001",2:"0010",3:"0011",4:"0100",5:"0101",6:"0110",7:"0111",8:"1000",9:"1001",A:"1010",B:"1011",C:"1100",D:"1101",E:"1110",F:"1111"}
vHex := Format("{:X}", vNum << 1 >> 1)
;vHex := "1234"
vBin := ""
Loop, Parse, vHex
	vBin .= oArray[A_LoopField]
;MsgBox, % vBin
vBin := LTrim(vBin, "0")
MsgBox, % vBin
return
[Some notes on UInt64:]
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 60#p171860
[Thanks also for this other post:]
Choose integer size - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13&t=42220
[lexikos linked to this article which has a nice image explaining a (justifiable) quirk of the right arithmetic shift:]
Arithmetic shift - Wikipedia
https://en.wikipedia.org/wiki/Arithmetic_shift
[A further link:]
Changed bitwise-NOT (~) to always treat number as 64-bit. · Lexikos/AutoHotkey_L@247bf90 · GitHub
https://github.com/Lexikos/AutoHotkey_L ... d38240fb35
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SetFormat, Integer, Unsigned

04 Jan 2018, 19:13

Here's the explanation.
Variables and Expressions
https://autohotkey.com/docs/Variables.htm
•Commands, functions, and expressions that accept numeric inputs generally support 15 digits of precision for floating point values. For integers, 64-bit signed values are supported, which range from -9223372036854775808 (-0x8000000000000000) to 9223372036854775807 (0x7FFFFFFFFFFFFFFF). Any integer constants outside this range are not supported and might yield inconsistent results. By contrast, arithmetic operations on integers wrap around upon overflow (e.g. 0x7FFFFFFFFFFFFFFF + 1 = -0x8000000000000000).
So I believe that your dec2bin function should work fine, although it's not always easy to assign the number you want when it's: over 0x7FFFFFFFFFFFFFFF and at most 0xFFFFFFFFFFFFFFFF.

Code: Select all

q:: ;test assign UInt64
vNum := 2**63
;vNum := 0x8000000000000000 ;this is not a valid assignment in AHK
MsgBox, % !!(vNum & (2**63))
MsgBox, % vNum
MsgBox, % Format("0x{:X}", vNum)

vNum := 0x7FFFFFFFFFFFFFFF + (2**63)
;vNum := ((2**63) * 2) - 1
;vNum := ((2**63) << 1) - 1
;vNum := (2**64) - 1 ;doesn't work
;vNum := -1
;vNum := 0xFFFFFFFFFFFFFFFF ;this is not a valid assignment in AHK
MsgBox, % !!(vNum & (2**63))
MsgBox, % vNum
MsgBox, % Format("0x{:X}", vNum)
;MsgBox, % Format("0x{:X}", vNum << 1 >> 1)
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: SetFormat, Integer, Unsigned  Topic is solved

06 Jan 2018, 22:50

This has been addressed by the latest v2 updates:
see https://autohotkey.com/boards/viewtopic ... 02#p193102
Thank you Lexikos
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SetFormat, Integer, Unsigned

07 Jan 2018, 00:47

- Btw Wolfram Alpha is useful for confirming that numeric values are equal.
0x8000000000000000 - 9223372036854775808 - Wolfram|Alpha
http://www.wolframalpha.com/input/?i=0x ... 6854775808

- Thanks for the heads-up nnnik.
- Thank you too lexikos. You're making changes faster than I can think of them.
- I think one or two of the recent changes have been related to threads that I've been in, nice to know that you read some of my posts.
- Fallbacks for 'does not exist in the current keyboard layout' is one idea that I just added to my simplified and restructured Wish List 2.0:
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13&t=36789
- All of this is great because I've been working on a mathematics tutorial, and the updates have helped clarify a few important concerns.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 20 guests