AutoHotkey Community

It is currently May 27th, 2012, 8:07 am

All times are UTC [ DST ]




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 44 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: October 21st, 2006, 9:13 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
For when you need to make those quick conversions...
e.g. toBin(0x5e3c) = 00000101111000111100; toBin(-16) = 11110000; toInt(10111) = 23; toInt(00111111) = 63

Code:
toInt(b, s = 0, c = 0) {
   Loop, % l := StrLen(b) - c
      i += SubStr(b, ++c, 1) * 1 << l - c
   Return, i - s * (1 << l)
}

toBin(i, s = 0, c = 0) {
   l := StrLen(i := Abs(i + u := i < 0))
   Loop, % Abs(s) + !s * l << 2
      b := u ^ 1 & i // (1 << c++) . b
   Return, b
}


Note: toInt() will return signed values if the second parameter is true/1; the second parameter of toBin() should be how many least-significant bits to return (false/0 = to the nearest 4 bits) e.g. toInt(111110000001, true) = -127, toBin(12, 5) = 01100

Download (<1 KiB)

_________________
GitHubScriptsIronAHK Contact by email not private message.


Last edited by polyethene on January 5th, 2007, 11:05 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 3:50 am 
Offline

Joined: February 13th, 2006, 10:40 pm
Posts: 389
Location: Utah
I like it!

I think that your title is misleading. "Lite" is an understatement! :D

One of the weakest areas of my coding is that my code is very un-optimized and usualy a lot longer and more complex than it needs to be. From studying examples like this and stuff that people like Laszlo write helps me to learn about how to optimize things.

_________________
Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 4:28 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
There were a few binary conversion routines already posted, like in here. The ones above are very short and nice. I have been using a little longer versions, which handle negative numbers and you can specify the number of bits.
Code:
Num2Bin(n,bits=0) {     ; Return LS "bits" of binary representation of "n"
   IfLess bits,1, Loop  ; n < 0: leading 1's are omitted. -1 -> 1, 0 -> 0
      {
         b := n&1 b
         n := n>>1
         If (n = n>>1)
            Break
      }
   Else Loop %bits%
      {
         b := n&1 b
         n := n>>1
      }
   Return b
}

Bin2Num(bits,neg="") {  ; Return number converted from the binary "bits" string
   n = 0                ; If "neg" is not 0 or empty, 11..1 assumed on the left
   Loop Parse, bits
      n += n + A_LoopField
   Return n - !(neg<1)*(1<<StrLen(bits))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 6:04 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Veovis wrote:
"Lite" is an understatement!
Lite in the sense that it's quite compact ;)

Laszlo wrote:
I have been using a little longer versions, which handle negative numbers and you can specify the number of bits.
I've updated toBin() with Abs(p) + (!p * StrLen(Abs(n)) << 2) to return a specified number of bits. I also replaced modulo 2 with & 1 (can't believe I forgot this). I copied your idea to make toInt() support negative numbers by using n - (1 << StrLen(b)) * s, I thought inverting bits was the only way but this is great.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 7th, 2006, 8:18 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
I wanted to see if I could map hex to decimal using the Asc table. Here's what I came up with (not optimized):
Code:
toDec(i) {
   j := RegExReplace(i, "^-?0x")
   Loop, % StrLen(j) {
      x := *(&j + A_Index - 1)
      d += 16 ** (StrLen(j) - A_Index) * (x - 48 - 7 * (x > 64) - 32 * !(x & 96 ^ 96))
   }
   Return, Chr(45 << (i < 0) - 1 - InStr(d, "-")) . d
}

Just a quick result from one of my many trivial experiments :)

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 5th, 2007, 11:06 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
I just updated the function with an LSB-skip parameter ('c'). Previous versions are still available.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 5th, 2007, 11:34 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The following line of your new function is not guaranteed to work in the next release of AHK. (See here)
Code:
i += SubStr(b, ++c, 1) * 1 << l - c

You could never know, what the following line does:
Code:
l := StrLen(i := Abs(i + u := i < 0))
"+" is of higher priority than ":=", so first i+u is computed, but it is not a left value, so it were a syntax error. However, AHK has a bunch of semi- or un-documented rules to handle such code. In this case, the precedence of ":=" is raised, and first "u:=i<0" is computed, resulting in the new value of u, added to i. Because the applied rule is not described in any details, this code might provide different results in the next release of AHK.

I am sorry, Titan! Creative coding is not supported. :cry:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 12:02 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Laszlo wrote:
The following line of your new function is not guaranteed to work in the next release of AHK. [i += SubStr(b, ++c, 1) * 1 << l - c]
Why? Here's the sequential evaluation order:
  1. c is incremented
  2. Character from SubStr is obtained
  3. l subtracted by c
  4. 1 bit shift left by previous value
  5. SubStr value multiplied by previous value
  6. i assigned to previous value
I don't see how that could change. If what you said was about the prefix increment I doubt it's priority will be reordered.

Laszlo wrote:
Because the applied rule is not described in any details, this code might provide different results in the next release of AHK. [l := StrLen(i := Abs(i + u := i < 0))]
I'm no fan of terse and rigid syntax rules. If it happens (and I certainly hope not), parenthesis can be added for an explicit evaluation order.

Laszlo wrote:
I am sorry, Titan! Creative coding is not supported. :(
I'm confused, aren't you conservative here...

Chris wrote:
PHP and C++ implement a subset of this behavior: neither of them has a strict operator precedence table. Instead, they use a rule-based approach because it enhances convenience and intuitiveness. You argue that they do the opposite, which they do, but only in cases that are much rarer.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 12:23 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Titan wrote:
Laszlo wrote:
The following line of your new function is not guaranteed to work in the next release of AHK. [i += SubStr(b, ++c, 1) * 1 << l - c]
Why?
You have the shift-left operator. Is its left-hand side or right hand side is evaluated first? Chris refused to commit to either one, but they lead to different results. You must not assume that ++c is evaluated earlier than 1-c. It is a pity, but if a variable appears more than once in an expression, its value must not be changed by a side effect (:=, ++ etc.).
Titan wrote:
I'm no fan of terse and rigid syntax rules. If it happens (and I certainly hope not), parenthesis can be added for an explicit evaluation order.
To be sure, add them now.
Titan wrote:
I'm confused, aren't you conservative here...
I am. Some newbie copies this line to his script, where it will not work, and we have to debug his code in the ask-for-help section, a year from now. Can we say, please use parentheses? It worked for a code guru, why and how should he change it?
Titan wrote:
Chris wrote:
PHP and C++ implement a subset of this behavior: neither of them has a strict operator precedence table. Instead, they use a rule-based approach because it enhances convenience and intuitiveness.
There is a significant difference. Those languages precisely specify the rules, AHK does not. Their users can rely on these rules for a long time, we cannot, since they are undocumented features.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 12:29 am 
Offline

Joined: November 13th, 2004, 4:08 am
Posts: 2951
Location: Minnesota
Laszlo wrote:
I am. Some newbie copies this line to his script, where it will not work, and we have to debug his code in the ask-for-help section, a year from now. Can we say, please use parentheses? It worked for a code guru, why and how should he change it?


I already directly countered this argument in this topic. Do you want to debate it or just advertise your view? Because you still haven't responded.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 12:45 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
@jonny: Don't we agree? I tried to convince Titan not to write obfuscated code, because it will cause us extra work later. I didn't say (here) that AHK should not allow these funny constructs, but beg not to use them.

I gave up arguing in the other thread. I should be happy having whatever AHK can offer. Up to a year ago I used AHK in my day-to-day work, prototyping algorithms, put together some quick-and-dirty automating tasks, but not any more. I still have my thousand line main script, with keyboard remap, analog clock, calendar, clipboard management, etc. always running, but AHK's goal to allow simple scripts to be even simpler is not for me. I just have different needs.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 1:02 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Laszlo wrote:
You have the shift-left operator. Is its left-hand side or right hand side is evaluated first? Chris refused to commit to either one
If I'm not mistaken, rhs like in all other languages. Chris would not frivolously morph the language without regard for compatibility; if something does change it will be stated in the release notes.

Laszlo wrote:
You must not assume that ++c is evaluated earlier than 1-c.
It's l - c (not 1). According to the documentation pre-increment/decrement "is performed immediately".

Laszlo wrote:
It is a pity, but if a variable appears more than once in an expression, its value must not be changed by a side effect (:=, ++ etc.).
Why not? If we know the order or experiment with trial & error it shouldn't be a problem.

Laszlo wrote:
To be sure, add them now.
There's no need. We'll know if and when expression handling changes.

Laszlo wrote:
Some newbie copies this line to his script, where it will not work, and we have to debug his code in the ask-for-help section, a year from now.
How many posts in the Ask For Help section concern complex expressions?! There's no need to be paranoid over something like that, the noob can invent his/her own method if nobody's willing to help.

Laszlo wrote:
Those languages precisely specify the rules, AHK does not.
If we know AHK follows PHP/C++ we can apply their rules.

Laszlo wrote:
I tried to convince Titan not to write obfuscated code, because it will cause us extra work later.
lol, no it won't... nobody's asking you to moderate the forums or take on customer support.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 2:14 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Titan wrote:
if something does change it will be stated in the release notes.
…and you go back an update all of your posted scripts, if necessary? It looks better not relying on undocumented features. Furthermore, changing undocumented features do not always get documented.
Titan wrote:
According to the documentation pre-increment/decrement "is performed immediately".
Immediately when? When the corresponding term is evaluated, of course. In "*p + *(++p)" the first reference is normally the un-incremented p, only after using it, in the evaluation of the second term will p be changed. Otherwise both terms referenced the same memory.
Laszlo wrote:
It is a pity, but if a variable appears more than once in an expression, its value must not be changed by a side effect (:=, ++ etc.).
Titan wrote:
Why not? If we know the order or experiment with trial & error it shouldn't be a problem.
Until the next release. Then your script might stop working, because some undocumented feature changed.
Laszlo wrote:
To be sure, add them now.
Titan wrote:
There's no need. We'll know if and when expression handling changes.
Again, when something undocumented changes, and remains undocumented, it will not show up in the list of changes.
Titan wrote:
How many posts in the Ask For Help section concern complex expressions?! There's no need to be paranoid over something like that, the noob can invent his/her own method if nobody's willing to help.
The tricky side-effects, precedence raising, etc. just have been introduced. But I have seen many expression related questions, mostly about = or :=, the need of %, and such.
Titan wrote:
If we know AHK follows PHP/C++ we can apply their rules.
But we don't know anything, because these rules in AHK are undocumented. It was sufficient documentation to tell, that expressions would follow the rules of PHP, but it is not the case.
Laszlo wrote:
I tried to convince Titan not to write obfuscated code, because it will cause us extra work later.
Titan wrote:
lol, no it won't... nobody's asking you to moderate the forums or take on customer support.
Are you saying that I should stop answering beginners' questions? Maybe you are right…


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 2:48 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Laszlo wrote:
…and you go back an update all of your posted scripts, if necessary?
I guess I'll have to. In the mean time I prefer to use the current features that are available, like most (99%) other users.

Laszlo wrote:
changing undocumented features do not always get documented.
As far as I know Chris always mentions possible script-breaking updates in Announcements.

Laszlo wrote:
In "*p + *(++p)" ... both terms referenced the same memory.
I haven't tried it but in my script ++c comes first anyway.

Laszlo wrote:
Again, when something undocumented changes, and remains undocumented, it will not show up in the list of changes.
I'm not obsessive over compatibility issues. If I find something not working I'll try fix it with extra parenthesis. Your advice is good nevertheless and I'll bear it in mind for future scripts, thanks.

Laszlo wrote:
I have seen many expression related questions, mostly about = or :=, the need of %, and such.
I have only seen queries relating to :=/% and none to do with evaluation order specifically (which is what this discussion is about right).

Laszlo wrote:
But we don't know anything, because these rules in AHK are undocumented.
Chris mentioned it on the forum, are you saying that isn't credible?

Laszlo wrote:
Laszlo wrote:
I tried to convince Titan not to write obfuscated code, because it will cause us extra work later.
Titan wrote:
lol, no it won't... nobody's asking you to moderate the forums or take on customer support.
Are you saying that I should stop answering beginners' questions? Maybe you are right…
'work' here implies forced duties, which is deceptive. You don't have to help noobs who use my code (which has never happened thus far). You are free to do as you wish but I appreciate your input and expertise, as always.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 6th, 2007, 3:49 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Titan wrote:
Chris mentioned [that AHK follows PHP/C++ rules] on the forum, are you saying that isn't credible?
I did not see Chris saying that those rules are followed exactly in AHK, only that expression evaluation in AHK is not solely determined by the precedence table, but also by some rules. This is similar in PHP/C++. In fact, the AHK rules are not exactly the same.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 44 posts ]  Go to page 1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group