Page 1 of 2
Add Binary - 32 bits
Posted: Fri Dec 31, 2021 1:15 pm
by Andy01
Hi Everyone, All the best in the new year and in the future

I'm trying to reduce all the functions I have written for SHA-256.
The add binary function I have written needs to be shortened as it gets called most of the time. Can anyone reduce this code using LiveCodes inbuilt functions? The function needs to return a 32 bit number

Any help in reducing the function would be awesome

Let's see what you've got
-- v1 and v2 are 32 bit numbers
function ADDBINARY v1,v2
put "00000000000000000000000000000000" into v0
repeat with c = 32 down to 1
put char c of v0+char c of v1+char c of v2 into n
if n = "0" then put "0" before output else if n = "1" then put "1" before output
else
if n = "2" then put "0" before output else if n = "3" then put "1" before output
put "1" into char c-1 of v0
end if
end repeat
return output
end ADDBINARY
Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 3:06 pm
by FourthWorld
What are you doing with SHA256 that isn't included in the LC engine?
Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 6:41 pm
by richmond62
-
Personally I would add the 2 numbers and then convert the result into binary:
-
-
Code: Select all
on mouseUp
ask "first number? "
put it into v1
ask "second number? "
put it into v2
put (v1 + v2) into REZ
put REZ into fld "fREZ"
put baseConvert(REZ, 10, 2) into fld "fBinREZ"
end mouseUp
I hope I did not miss the point completely.
Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 7:40 pm
by Andy01
Hi Richard Happy New Year, Yes SHA-256 is not included in the LiveCode engine that's why I created my own functions to accomplish that. This is the last function I need to minimise to speed up the processing of the blocks. Maybe the inbuilt functions in LiveCode can't do what I need

Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 7:53 pm
by richmond62
v1 and v2 are 32 bit numbers
If that means they are base 2 numbers then you can baseConvert then to decimal, add them, and then baseConvert the product
back to binary.
-

- btG.jpeg (67.48 KiB) Viewed 7256 times
Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 8:06 pm
by FourthWorld
Andy01 wrote: ↑Fri Dec 31, 2021 7:40 pm
Hi Richard Happy New Year, Yes SHA-256 is not included in the LiveCode engine that's why I created my own functions to accomplish that. This is the last function I need to minimise to speed up the processing of the blocks. Maybe the inbuilt functions in LiveCode can't do what I need
LiveCode offers SHA-256 among its many hash options with the messageDigest function. The full list of supported hashes in v9.0 and later is:
"SHA3-224"
"SHA3-256"
"SHA3-384"
"SHA3-512"
"SHA-224"
"SHA-256"
"SHA-384"
"SHA-512"
"SHA-1" - Use only for backwards compatibility
"MD5" - Use only for backwards compatibility
You'll also find several 256-bit ciphers in the list returned by the cipherNames function, which can be used with the encrypt command.
Is there something beyond hashing and encrypting you need 256-bit strength for?
Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 8:29 pm
by richmond62
Right there in the Dictionary: the only problem is knowing
what to search for:
-
Re: Add Binary - 32 bits
Posted: Fri Dec 31, 2021 8:39 pm
by Klaus
True, that should definitively be listed under -> encrypt
And under -> cyphernames
Re: Add Binary - 32 bits
Posted: Sat Jan 01, 2022 12:48 am
by Andy01
Thanks for the info the only thing is I don't have those functions listed in the dictionary
Re: Add Binary - 32 bits
Posted: Sat Jan 01, 2022 1:04 am
by Andy01
Thanks guys for your all your help I'm only using LiveCode version 652 so I don't have those inbuilt functions. It's okay I have it working so it is what it is

Re: Add Binary - 32 bits
Posted: Sat Jan 01, 2022 1:50 am
by Andy01
richmond62 wrote: ↑Fri Dec 31, 2021 7:53 pm
v1 and v2 are 32 bit numbers
If that means they are base 2 numbers then you can baseConvert then to decimal, add them, and then baseConvert the product
back to binary.
-
btG.jpeg
Thanks, I will give this another shot I think I had a problem with this, it wasn't converting to the correct length of bits, cheers and Happy New Year
Re: Add Binary - 32 bits
Posted: Sat Jan 01, 2022 7:12 am
by Andy01
I can live with this its slightly better than the previous function

-- v0 = 00000000000000000000000000000000
-- v1 = 01011011111000001100110100011001
-- v2 = 00110101100001110010011100101011
-- output = 10010001011001111111010001000100
function ADDBINARY v0,v1,v2
repeat with c = 32 down to 1
put char c of v0+char c of v1+char c of v2 into n
if n < "2" then
put n before output
else
put n-2 before output
put "1" into char c-1 of v0
end if
end repeat
return output
end ADDBINARY
Re: Add Binary - 32 bits
Posted: Sat Jan 01, 2022 8:35 pm
by richmond62
I'm only using LiveCode version 652
Cripes.
get a more up-to-date Open Source (Free) version here:
https://archive.org/download/live-code- ... taller-mac
https://archive.org/download/live-code- ... -linux-x64
https://archive.org/download/live-code- ... ws-x86-x64
and, after you've tested things out and want to release something commercial, pay for
a licensed version.
Re: Add Binary - 32 bits
Posted: Sun Jan 02, 2022 6:07 am
by Andy01
richmond62 wrote: ↑Sat Jan 01, 2022 8:35 pm
I'm only using LiveCode version 652
Cripes.



Spot on richmond62

Yes until I want to release something I'll pay for a licensed version. Another thing is I'm using a LiveCode PPC version running on a PowerPC G4 laptop with only 512 MB of ram. I think that was the last version for the PPC. It's just a test computer to streamline the code that I write. I'm looking at the baseConvert function again that you mentioned. I can't force the numberFormat function to convert a number with leading or trailing zero's
It's not like hypertalks numberFormat

Anyway thanks again for your help, cheers mate

Re: Add Binary - 32 bits
Posted: Sun Jan 02, 2022 12:57 pm
by richmond62
It's just a test computer to streamline the code that I write.
I don't know where you live, but here, in Bulgaria, you can pick up a second-hand 64-bit PC for about 60 Euros, as I have just
done (I bought 3 for my second classroom in my school), and bung Xubuntu on it, which works without a hitch
with LiveCode right up to the recent commercial-only versions.
[The only snag, I have found, is that those second-hand computers only seem to last about 12 years!]
3 GB RAM, 512 GB hard drive . . .
30 minutes to install Xubuntu, another hour to install LC, GIMP, LibreOffice and other Open Source, mission-critical software.
Each of my school computers works out at about 100 Euros (60 for the computer, 25 for a 1024 x 768 second-hand monitor,
15 for keyboard, 0 for a ball 3 button mouse in my extensive box of "old rubbish").
Never, knowingly, bought any new hardware in the last 20 years.