| View previous topic :: View next topic |
| Author |
Message |
Corroder Grandmaster Cheater Supreme
Reputation: 75
Joined: 10 Apr 2015 Posts: 1668
|
Posted: Fri May 10, 2019 10:48 am Post subject: Handle negative 32-bit signed binary and octal |
|
|
Hi there,
Negative dec. value to hex:
| Code: | | print(("%x"):format(-2147458560)) -- result : ffffffff80006200 |
How to handle negative dec. (integer value) to 32-bit signed binary using Lua script?.
In python, console write:
| Code: | a = -2147458560
>>> bin(a & 0xffffffff) -- result : 0b10000000000000000110001000000000
|
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25864 Location: The netherlands
|
Posted: Fri May 10, 2019 12:50 pm Post subject: |
|
|
| Code: |
print(("%x"):format(-2147458560 & 0xffffffff))
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
Corroder Grandmaster Cheater Supreme
Reputation: 75
Joined: 10 Apr 2015 Posts: 1668
|
Posted: Fri May 10, 2019 9:37 pm Post subject: |
|
|
DB,
| Code: | | print(("%x"):format(-2147458560 & 0xffffffff)) |
-- result = 80006200
I check on windows 7 calculator :
Dec = -2147458560
Bin = 1111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0000 0000 0110 0010 0000 0000
So, I mean how to convert negative dec value to binary?.
If I use :
| Code: | function basen(n,b) -- handle Dec. Num. Convertion
n = math.floor(n)
if not b or b == 10 then return tostring(n) end
local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local t = {}
local sign = ""
if n < 0 then
sign = "-"
n = -n
end
repeat
local d = (n % b) + 1
n = math.floor(n / b)
table.insert(t, 1, digits:sub(d,d))
until n == 0
return sign .. table.concat(t,"")
end
function hex2bin(s)
dec = tonumber(s,16)
return basen(dec,2)
end
-- dec= -2147458560 is 'ffffffff80006200' on hex
print(hex2bin('ffffffff80006200'))
-- result : -1111 1111 1111 1111 0011 1100 0000 000
|
The binary result is different from windows 7 calculator result. Any solution in Lua script?.
I noted Lua version on CE 6.8.3 is Lua 5.3 with not handle bitwise operation anymore.
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25864 Location: The netherlands
|
Posted: Sat May 11, 2019 12:01 am Post subject: |
|
|
I don't know why you think it doesn't handle bitwise operation, as the & operator is a bitwise operator
As for why your result is different from the calculator, that's because your basen function is showing it as a negative value
Flipping from negative to positive inverts all bits and then does +1 which explains the different value
Anyhow, here's a 32-bit value to bin convertor. (change 31 to 63 for 64-bit values)
| Code: |
function dwordtobin(v)
local result=''
--I can do the value/2 and value%2 thing to get the string, but just showing bitwise operators here
for i=0,31 do
if ((v >> i) & 1)==1 then
result='1'..result
else
result='0'..result
end
end
return result
end
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
Corroder Grandmaster Cheater Supreme
Reputation: 75
Joined: 10 Apr 2015 Posts: 1668
|
Posted: Sat May 11, 2019 3:16 am Post subject: |
|
|
| Quote: | | Flipping from negative to positive inverts all bits and then does +1 which explains the different value |
Ah, it reminds me of "two's complement"
| Code: | | Two's complement is the way every computer I know of chooses to represent integers. To get the two's complement negative notation of an integer, you write out the number in binary. You then invert the digits, and add one to the result |
DB thanks for the function, it works as I wish both 32-bit and 64-bit by as your suggestion.
Actually, I working in a project by reference to windows calculator programmer mode. I did make one in VB.Net, but I want to learn to make it in CE Lua.
Attached is part of my CE Lua calculator. Need someone to test and help to give better functions for Hex, Dec, Oct, and Bin converter vise versa.
I think there is some problem with Bin to Dec function in the CT file attached (7kb).
(Can't attached here, so here is the link to download):
--- old link deleted
Regards,
EDIT: Problem SOLVED, new CT file and download link as below:
https://mega.nz/#!a1cVAKCY!UviohD-UQJdL74ia1py0EoUwhllgn1vBbtRXhfKm51o
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
| Back to top |
|
 |
|