2's complement of Hexadecimal numbers

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

2's complement of Hexadecimal numbers

Post by capellan » Mon May 17, 2021 5:31 am

Hi All,

Did Livecode includes a function to calculate the
2's complement of an Hexadecimal number?

https://www.cs.cornell.edu/~tomf/notes/ ... scomp.html

Thanks in advance!

Al

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9835
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: 2's complement of Hexadecimal numbers

Post by FourthWorld » Mon May 17, 2021 6:57 am

Seems useful for lower level computation, by what are the applications of such a function in a scripting language?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: 2's complement of Hexadecimal numbers

Post by capellan » Mon May 17, 2021 7:02 am

I will use this for calculating the negative height of BMP files.
https://stackoverflow.com/questions/373 ... age-in-hex

Mac OSX Preview and GIMP opens fine these BMP with
Transparency and negative Height.

https://drive.google.com/file/d/1XiBVh6 ... sp=sharing

EDIT: This code could work, but requires more testing:

Code: Select all

put baseconvert(((bitnot 326) + 1),10,16)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9835
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: 2's complement of Hexadecimal numbers

Post by FourthWorld » Mon May 17, 2021 10:02 am

Curious. It hadn't occurred to me that an image format would use negative numbers. Thanks.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9662
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: 2's complement of Hexadecimal numbers

Post by dunbarx » Mon May 17, 2021 2:47 pm

Hi.
Simple to write a function. With a field with a hex string and this in a button script:

Code: Select all

on mouseUp
   get baseConvert(fld 1,16,2)
   replace "0" with "x" in it
   replace 1 with 0 in it
   replace "x" with 1 in it
   
   put baseConvert(it,2,10) into temp
   answer baseConvert(temp + 1,10,2)
end mouseUp
Craig

EDIT.

Rewrote handler because I did not see at first the initial string was in hex.

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: 2's complement of Hexadecimal numbers

Post by capellan » Thu May 20, 2021 5:46 am

Hi Craig,

Many Thanks for posting your script!
I ended up using this handler:

Code: Select all

on mouseUp 
   put baseconvert(((bitnot fld 1) + 1),10,16)
end mouseUp

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9662
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: 2's complement of Hexadecimal numbers

Post by dunbarx » Thu May 20, 2021 2:24 pm

Hi.

Yes, "bitNot" reduces the number of lines required to, er, not the bits.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”