lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dear bit-loving Lua developers...

I finally broke down and added bitwise operators (&, |, and # - see below) to 
the lexer/parser/virtual machine. I also include in this patch my previous 
hexadecimal patch, since most of the time you'll want to do both together. I 
have uploaded the patch to the Power Patches section of the lua-users wiki:

  http://lua-users.org/wiki/LuaPowerPatches

Here's the abstract:

Bitwise operators and hexadecimal support

This patch adds the following features to Lua 5.x: 

o Hexadecimal support for 0xXXX in numeric literals. 
o Hexadecimal support for '\xXX' characters within strings. 
o Infix bitwise operators for AND (&), OR (|) and XOR (#). 
o Unary bitwise negation using # alone. 

NOTE: This patch adds opcodes to the Lua virtual machine. The two major 
consequences of this are that (1) the result cannot be called 'Lua', and (2) 
compiled code will not run on interpreters that do not include this patch. 

After applying the patch, try running the following: 

> hex=function(n) print("0x"..string.format("%X",n)) end
> hex(0x54&0x55)
0x54
> hex(0x54|0x66)
0x76
> hex(0x54#0x66)
0x32
> hex(#0x45)    
0xFFFFFFBA
> print("Hel\x6c\x6f world\x21")
Hello world!
> 

This has been tested on Lua-5.0, -5.0.1, and -5.0.2.

Have fun!

Doug Rogers

-- 
--__-__-____------_--_-_-_-___-___-____-_--_-___--____
Doug Rogers - ICI - V:703.893.2007x220 www.innocon.com
-_-_--_------____-_-_-___-_--___-_-___-_-_---_--_-__-_