Page 1 of 1
2's complement of Hexadecimal numbers
Posted: Mon May 17, 2021 5:31 am
by capellan
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
Re: 2's complement of Hexadecimal numbers
Posted: Mon May 17, 2021 6:57 am
by FourthWorld
Seems useful for lower level computation, by what are the applications of such a function in a scripting language?
Re: 2's complement of Hexadecimal numbers
Posted: Mon May 17, 2021 7:02 am
by capellan
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)
Re: 2's complement of Hexadecimal numbers
Posted: Mon May 17, 2021 10:02 am
by FourthWorld
Curious. It hadn't occurred to me that an image format would use negative numbers. Thanks.
Re: 2's complement of Hexadecimal numbers
Posted: Mon May 17, 2021 2:47 pm
by dunbarx
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.
Re: 2's complement of Hexadecimal numbers
Posted: Thu May 20, 2021 5:46 am
by capellan
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
Re: 2's complement of Hexadecimal numbers
Posted: Thu May 20, 2021 2:24 pm
by dunbarx
Hi.
Yes, "bitNot" reduces the number of lines required to, er, not the bits.
Craig