Add Binary - 32 bits

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Add Binary - 32 bits

Post by Andy01 » Fri Dec 31, 2021 1:15 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Add Binary - 32 bits

Post by FourthWorld » Fri Dec 31, 2021 3:06 pm

What are you doing with SHA256 that isn't included in the LC engine?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10080
Joined: Fri Feb 19, 2010 10:17 am

Re: Add Binary - 32 bits

Post by richmond62 » Fri Dec 31, 2021 6:41 pm

SShot 2021-12-31 at 19.31.21.png
-
Personally I would add the 2 numbers and then convert the result into binary:
-
SShot 2021-12-31 at 19.38.33.png
-

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.
Attachments
Binary Biter.livecode.zip
Here's the stack.
(1.03 KiB) Downloaded 181 times

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Add Binary - 32 bits

Post by Andy01 » 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 :(

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10080
Joined: Fri Feb 19, 2010 10:17 am

Re: Add Binary - 32 bits

Post by richmond62 » 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
btG.jpeg (67.48 KiB) Viewed 7238 times

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Add Binary - 32 bits

Post by FourthWorld » Fri Dec 31, 2021 8:06 pm

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10080
Joined: Fri Feb 19, 2010 10:17 am

Re: Add Binary - 32 bits

Post by richmond62 » Fri Dec 31, 2021 8:29 pm

Right there in the Dictionary: the only problem is knowing
what to search for:
-
SShot 2021-12-31 at 21.28.10.png

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Add Binary - 32 bits

Post by Klaus » Fri Dec 31, 2021 8:39 pm

True, that should definitively be listed under -> encrypt
And under -> cyphernames

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Add Binary - 32 bits

Post by Andy01 » Sat Jan 01, 2022 12:48 am

Thanks for the info the only thing is I don't have those functions listed in the dictionary

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Add Binary - 32 bits

Post by Andy01 » Sat Jan 01, 2022 1:04 am

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 :roll:

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Add Binary - 32 bits

Post by Andy01 » Sat Jan 01, 2022 1:50 am

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

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Add Binary - 32 bits

Post by Andy01 » Sat Jan 01, 2022 7:12 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10080
Joined: Fri Feb 19, 2010 10:17 am

Re: Add Binary - 32 bits

Post by richmond62 » Sat Jan 01, 2022 8:35 pm

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.

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Add Binary - 32 bits

Post by Andy01 » Sun Jan 02, 2022 6:07 am

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 😁

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10080
Joined: Fri Feb 19, 2010 10:17 am

Re: Add Binary - 32 bits

Post by richmond62 » Sun Jan 02, 2022 12:57 pm

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.

Post Reply