Jump to content

Bitwise logic and base 2 extraction


Go to solution Solved by Nine,

Recommended Posts

Simple question: I'm extracting all individual states from a WinGetState() function as base 2 numbers concatenated into a string using space separators, and I was wondering if there's a shorter way of doing something like this (maybe a ? after the function, enclosing it between brackets to force it into a 0 or 1 value, or something along those lines) - the reason is not important, I'm just curious:

BitAND($Sta, 4) / 4
Link to comment
Share on other sites

I wonder if anyone have any clue about what are you asking. So you have a string with all possible states returned by WinGetState() as base 2 numbers that are concatenated into a string using space separator between the states.

$Sta = '1 10 100 1000 10000 100000'

I assume something like this. Now what?

When the words fail... music speaks.

Link to comment
Share on other sites

  • Solution

ntdll.dll has numerous functions to convert (u)int to string (and vice versa) . Example :

DllCall("ntdll.dll", "str:cdecl", "_ui64toa", "uint64", $integer, "str", "", "int", $base)

by using a $base = 2, you get a binary string of the $integer

Link to comment
Share on other sites

6 hours ago, Andreik said:

I wonder if anyone have any clue about what are you asking. So you have a string with all possible states returned by WinGetState() as base 2 numbers that are concatenated into a string using space separator between the states.

$Sta = '1 10 100 1000 10000 100000'

I assume something like this. Now what?

My bad, I thought I was clear enough in my post (I did say "all individual states" and not "all possible states", after all) - no need to be confrontational about it. Since you asked though, I have this (where $wHan is a window handle):
 

$wSta = WinGetState($wHan)
  $wBit = BitAND($wSta, 1) / 1 & " " & BitAND($wSta, 2) / 2 & " " & BitAND($wSta, 4) / 4 & " " & BitAND($wSta, 8) / 8 & " " & BitAND($wSta, 16) / 16 & " " & BitAND($wSta, 32) / 32

And I was simply looking for a shorter, preferably AutoIt built-in function that would make converting the output of WinGetState() into a base 2 number or string that I could manipulate further by adding the space separators and reversing the bit order according to my needs, that's all.

 

 

Link to comment
Share on other sites

This is what I am using to convert uint to bin string with spaces :

Func UIntToBinary($nUInt)
  Local $sBin = DllCall("ntdll.dll", "str:cdecl", "_ui64toa", "uint64", $nUInt, "str", "", "int", 2)[0]
  Local $iLen = Ceiling(StringLen($sBin) / 4) * 4
  $sBin = _StringRepeat("0", $iLen - StringLen($sBin)) & $sBin
  Return StringRegExpReplace($sBin, "(.{4})", "\1 ")
EndFunc

 

Link to comment
Share on other sites

6 hours ago, Nine said:

ntdll.dll has numerous functions to convert (u)int to string (and vice versa) . Example :

DllCall("ntdll.dll", "str:cdecl", "_ui64toa", "uint64", $integer, "str", "", "int", $base)

by using a $base = 2, you get a binary string of the $integer

 Many thanks, it looks like just what I need:

$wBit = StringRegExpReplace(StringReverse(StringFormat("%06d", DllCall("ntdll.dll", "str:cdecl", "_ui64toa", "int64", WinGetState($wHan), "str", "", "int", 2)[0])), "(.)(?!$)", "\1 ")

By the way, is there any difference in terms of performance if I first do ($wHan is a window handle): 

$wSta = WinGetState($wHan)

before using $wSta in the $wBit assignment? Besides clarity, better debugging and so on, that is.

It's a pity though that AutoIt has all those many binary functions and not a single one that does base conversion (apart from UDFs), e.g. like ToBase(value, base, positions) - and I looked quite a bit on the forums before posting this question.

EDIT: Just saw your reply. Yep, that's a similar approach to what I've ended up with. It does its job, it fits one line, so the "no clutter / scrollbars" objective is achieved for my code.

 

Edited by Yincognito
Corrected typo.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...