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:

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)
> hex(0x54|0x66)
> hex(0x54#0x66)
> hex(#0x45)    
> 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