bitwise comparisons: logical XOR, OR, AND
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

bitwise comparisons: logical XOR, OR, AND

Started by Mardeg, 19 August, 2005, 22:03:43

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mardeg

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?

Dessamator

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
Ignorance is Bliss.

Mardeg

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

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
Everything could have been anything else and it would have just as much meaning.

Mardeg

#4
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?

bastya_elvtars

What is odd(a) intended to do?
Everything could have been anything else and it would have just as much meaning.

Mardeg

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.

bluebear

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.
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

bluebear

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).
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

Mardeg

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.

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
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

bluebear

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 mod is not there cause lua already has it.
Sincerely,
bluebear
--
http://www.thewildplace.dk/ is is closed - Use the following mirrors instead
http://bluebear.psycho-chihuahua.net
http://pxextension.piratez.dk/
[Lua extensions - Chat stats - YnHub PMSpy - DC Source code - and more]

Mardeg

bluebear, you are a mighty legend before your time  :)

SMF spam blocked by CleanTalk