Jump to content
Sign in to follow this  
czardas

Binary, strings or numbers

Recommended Posts

czardas

I have asked a similar question to this before, but I don't think I worded myself properly, or perhaps I didn't understand the answer. I nearly always give up and convert binary it to a string so I can work with it. What is the advantage of using bitwise functions when I have to convert the results to a string anyway.

$iValue = 0xFAF
ConsoleWrite(_IntToBin($iValue) & @CRLF)
 
Func _IntToBin($iValue)
    Local $sOut
    For $i = 11 To 0 Step -1
        $sOut &= BitAND(BitShift($iValue, $i), 1) ; Convert to 12 digit string needed to spin, cut chop etc...
    Next
    Return $sOut
EndFunc

If I only want part of the output, how do I extract a number of zeros and ones from anywhere within the binary without converting it to a string? I don't see much advantage in having to convert a variable back and forth between data types several times.

What if I want to create a distribution curve out of ones and zeros.

00011000
00111100
11111111
========
11233211 <= bit distribution from 3 values (limited method)

It seems that this his would require a few loops with bitwise functions, but I could simply add the numbers together without all that fuss. 11000 + 111100 + 11111111 = 11233211

So did I hit a sore nerve, or am I missing something obvious? I'm not asking for code, I would just like your opinions.

Edited by czardas

Share this post


Link to post
Share on other sites
rcmaehl

If I understand correctly and you're trying to convert base 10 to base 2 then: I believe it's done by splitting the string then multiplying each bit value 1,2,4,8,16,32,64, etc. by the number of occurences of that bit, adding all the values together then converting that result into binary. I'll start coding an example of this if that's the case.

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
czardas

If I understand correctly and you're trying to convert base 10 to base 2 then: I believe it's done by splitting the string then multiplying each bit value 1,2,4,8,16,32,64, etc. by the number of occurences of that bit, adding all the values together then converting that result into binary. I'll start coding an example of this if that's the case.

No, nothing like that. Binary is boolean magic, but it seems you need something like string functions to realise its potential. The distribution example is just a simple trick. Edited by czardas

Share this post


Link to post
Share on other sites
rcmaehl

So what exactly are you asking. Are you asking how to get like 00100000 from 101101001 without use a string? I'm really just confused that's all.


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Share this post


Link to post
Share on other sites
czardas

I'm asking what advantages are there to using bitwise functions, when the binary needs converting to a different variable type in order to be able to manipulate the data as required? The speed gained using bitwise functions may be lost in conversion. I can't imagine any advantage at all with the bit distribution example.

Oh well! I admit the question is a bit vague. Perhaps I can get faster speeds creating things like a 12 bit structures using DllStructCreate instead of messing around with strings. I have yet to be convinced it will make the things I want to do any easier or quicker. I need to try though.

$a = DllStructCreate("align 4;byte;double")
DllStructSetData($a,1,0xFFF)
Edited by czardas

Share this post


Link to post
Share on other sites
MvGulik

Think your asking if its possible to do this

00011000
00111100
11111111
========
11233211 <= bit distribution from 3 values (limited method)
On hex-binary data directly, whiteout first converting the Hex-Binary data to a Bit-Binary string.

Don't see how ... if you need to be able to store data that can't be stored into a single Bit unit. => 2 + 3 values in your end result.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
czardas

The (nearly) normal distribution curve above is of lower priority to me (just a trick that I might use, or not). I really want to have the full functionality of string operations to manipulate binary. In particular spinning a loop of 12 bits. At the moment I do this with strings quite successfully, although it should also be easy to do it with BitShift. I'm not so familiar enough with DllStructCreate but I'm sure I can get my head around it. If I want to also concatenate or select any sized stream of bits from a (repeating infinite) loop of ones and zeros, then I don't see the advantage of having to convert from hex. Under certain circumstances I may have to change the size of the structure, if such things are possible. I guess I won't know if there are any advantages, unless I test these things out for myself.

I asked because I have the desire to do things in the proper way, but more importantly because I may be able to speed things up. Working in binary is totally necessary for some of the things I am making. I could continue to work with strings of ones and zeros, or try to create some string type functions to work on hex. I don't know what is best. I guess I'll only find out if I try.

I hope some of that made sense. :graduated:

I also don't know why, when I put hex in with DllStructSetData, DllStructGetData returns a decimal integer. I'm all confused. That needs converting back to what it was when it went in.

Edited by czardas

Share this post


Link to post
Share on other sites
MvGulik

... Just so you can compare your thoughts about this to mine ...

In general, doing bit-wise operations will always run into the limitation that the general used unit is the Byte.

Dropping the bits as separate byte/character in a String* therefor makes sens. Although the string part is not the important parts here. The storing of the separate bits in, and as, single byte units is.

*(exploded bits, to bytes, stream)

In relation to your spinner code. I think that only by looking at the processes there in a case by case study might reveal some cases where you could ditch (optimize) the 'Bit to String, and back again' part with something else.

For storing a exploded bits stream, It don't makes much difference for the data itself if its stored as String(), Binary() or DllStructCreate(). Which one is best used ... depends on how you need or like to access the data. ... Or which one is the easier, and/or faster one.

Think DllStructCreate() will quickly fail speed wise compared to String() and Binary(). Not sure about how String() and Binary() compare.

Edits: spelling ...

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites
czardas

I'm happy someone understands my question. I sometimes can't help thinking to myself that people looking at some of my code are perhaps bemused by the fact that I convert binary to strings. While I am often looking for ways to make my code run faster, the fact remains that string functions are generally not so fast. If there is a better way, then I want to try it out. But as you say, I guess it depends on circumstances. I can store my data in different ways. Which is best, less bytes or less conversion time? Conversion to strings may be necessary in some cases since the BitWise stuff seems to have its limitations. Thanks for your input. :graduated:

Share this post


Link to post
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
Sign in to follow this  

×