SetFormat, Integer, Unsigned Topic is solved

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

SetFormat, Integer, Unsigned

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
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: 37
Joined: 22 Apr 2017, 20:57

Re: SetFormat, Integer, Unsigned

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:

`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: 37
Joined: 22 Apr 2017, 20:57

Re: SetFormat, Integer, Unsigned

Alas, it wasn't that bad to fix.

`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.
jeeswg
Posts: 4785
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SetFormat, Integer, Unsigned

- 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.

[Some notes on UInt64:]
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=36789&p=171860#p171860
[Thanks also for this other post:]
Choose integer size - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=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
Changed bitwise-NOT (~) to always treat number as 64-bit. · Lexikos/AutoHotkey_L@247bf90 · GitHub
https://github.com/Lexikos/AutoHotkey_L/commit/247bf90fc8ae11e8f0a24a811527f4d38240fb35
jeeswg
Posts: 4785
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SetFormat, Integer, Unsigned

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.
nnnik
Posts: 3067
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: SetFormat, Integer, Unsigned  Topic is solved

see https://autohotkey.com/boards/viewtopic ... 02#p193102
Thank you Lexikos
Recommends AHK Studio
jeeswg
Posts: 4785
Joined: 19 Dec 2016, 01:58
Location: UK

Re: SetFormat, Integer, Unsigned

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

- 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.php?f=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.