PtokaX forum

Archive => HOW-TO's => Archived 5.1 boards => Old HOW-TO's => Topic started by: Mardeg on 19 August, 2005, 22:03:43

Title: bitwise comparisons: logical XOR, OR, AND
Post by: Mardeg on 19 August, 2005, 22:03:43
I don't see these functions in Lua. How would this be implemented with custom functions?
function XOR(x,y)

-- Insert code to return logical result of x XOR y

end


function OR(x,y)

-- Insert code to return  logical result of x OR y

end


function AND(x,y)

-- Insert code to return  logical result of x AND y


end

Also, are the reserved words case-insensitive or are we able to use these as functions since they are capitalised?
Title:
Post by: Dessamator on 19 August, 2005, 22:58:21
whaaa?, what the heck do u want those functions to return? and, or , are already existent on lua dont know why u want functions for that
Title:
Post by: Mardeg on 19 August, 2005, 23:51:50
Seriously unhelpful comment.

Anyway,  I've found something for the BITWISE comparisons (not the BOOLEAN comparisons) of OR, AND from the BSD cvs:
-- http://perforce.freebsd.org/fileLogView.cgi?FSPC=//depot/projects/soc2005/bsdinstaller/src/contrib/bsdinstaller/backend/lua/lib/bitwise.lua
-- lib/bitwise.lua
-- $Id: bitwise.lua,v 1.3 2005/04/11 02:21:37 cpressey Exp $
-- Package for (pure-Lua portable but extremely slow) bitwise arithmetic.

-- BEGIN lib/bitwise.lua --

module "bitwise"

--[[---------]]--
--[[ Bitwise ]]--
--[[---------]]--

local odd = function(x)
        return x ~= math.floor(x / 2) * 2
end

Bitwise = {}

Bitwise.bw_and = function(a, b)
        local c, pow = 0, 1
        while a > 0 or b > 0 do
                if odd(a) and odd(b) then
                        c = c + pow
                end
                a = math.floor(a / 2)
                b = math.floor(b / 2)
                pow = pow * 2
        end
        return c
end

Bitwise.bw_or = function(a, b)
        local c, pow = 0, 1
        while a > 0 or b > 0 do
                if odd(a) or odd(b) then
                        c = c + pow
                end
                a = math.floor(a / 2)
                b = math.floor(b / 2)
                pow = pow * 2
        end
        return c
end


All that is left to do is XOR
Title:
Post by: bastya_elvtars on 20 August, 2005, 00:57:23
XOR is either A or either B but never the both, right?

If so then

if (a and not b ) or (b and not a) then return WhatEverYouWant end
Title:
Post by: Mardeg on 20 August, 2005, 01:10:03
So a XOR b would be something like:
function XOR(a, b)

        local c, pow = 0, 1

        while a > 0 or b > 0 do

                if (odd(a) and not odd(b)) or (odd(b) and not odd(a)) then

                        c = c + pow

                end

                a = math.floor(a / 2)

                b = math.floor(b / 2)

                pow = pow * 2

        end

        return c

end

at a guess?
Title:
Post by: bastya_elvtars on 20 August, 2005, 01:15:10
What is odd(a) intended to do?
Title:
Post by: Mardeg on 20 August, 2005, 02:04:37
QuoteOriginally posted by bastya_elvtars
What is odd(a) intended to do?

Best to ask whoever is the author of that function, but I'd hazard a guess that it only returns true if the bit in question is odd and false if it is not.

I don't know if the functions work but if, for example, you wanted the bitwise result of 189 XOR 213:

XOR(189,213) should return 104 because

189 = 10111101
213 = 11010101
104 = 01101000

Notice the bits of 104 are only set to 1 if the 189 and 213 corresponding bits don't match.
Title:
Post by: bluebear on 20 August, 2005, 09:47:20
QuoteOriginally posted by Mardeg

bitwise comparisons: logical XOR, OR, AND

I don't see these functions in Lua. How would this be implemented with custom functions?


Tell me a good reason for why you want bitwise operators? If reason is good i'll implement thease for Ptokax/Lua.
Title:
Post by: bluebear on 20 August, 2005, 09:54:23
QuoteOriginally posted by bastya_elvtars
XOR is either A or either B but never the both, right?

If so then

if (a and not b ) or (b and not a) then return WhatEverYouWant end

Xor:
The result bit is 1 if either bit is 1 but not both bits; otherwise the result bit is 0
(that is, 1 Xor 0 = 1, 1 Xor 1 = 0).
Title:
Post by: Mardeg on 20 August, 2005, 13:48:09
QuoteOriginally posted by bluebear
Tell me a good reason for why you want bitwise operators? If reason is good i'll implement thease for Ptokax/Lua.

Discussion on this seems to suggest that lua 5.1 will have some functionality in this regard:

http://mailman.lyra.org/pipermail/scite-interest/2005-July/006031.html

but I don't think any reason is worth adding it to Ptokax/Lua core, unless you were thinking of another extension.

What I do with those above functions (tested now as working) is to get a game script to log into another hub (using the socket extension for ptokax) as a bot/user so the users in that hub can also play. I did this partly because the other hub owner is too lazy to become part of a network, and runs a non-ptokax hub. I know I could do this with an lua-scriptable client, or even mIRC, and have done in the past, but the other part of why I did it is.. because of the challenge to do it "because it's there" :)

http://wiki.dcpp.net/index.php/LockToKey#Lua_5.0.2B

If you want to implement it you'll have to come up with your own reason, sorry.
Title:
Post by: bluebear on 20 August, 2005, 15:52:06
Haveing BitWise in Lua core would be nice i think..
Maybe i used the wrong words.
I'm makeing a PxBITWISE.dll, an extension to PXLua.

It will feature

Xor, And, or, Lshitf, Rshift, mod
Title:
Post by: bluebear on 21 August, 2005, 13:53:45
QuoteOriginally posted by bluebear
Haveing BitWise in Lua core would be nice i think..
Maybe i used the wrong words.
I'm makeing a PxBITWISE.dll, an extension to PXLua.

It will feature

Xor, And, or, Lshitf, Rshift, mod

Here it is
http://board.univ-angers.fr/thread.php?threadid=5216&boardid=5&sid=74a1fd095d706758261aff6765a31afd&page=1#1 (http://board.univ-angers.fr/thread.php?threadid=5216&boardid=5&sid=74a1fd095d706758261aff6765a31afd&page=1#1) mod is not there cause lua already has it.
Title:
Post by: Mardeg on 22 August, 2005, 11:36:36
bluebear, you are a mighty legend before your time  :)