Simulate Bitwise Shift Operators in Lua

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Simulate Bitwise Shift Operators in Lua

Post by BlackBulletIV »

I've found a way to simulate the bitwise shift operators in Lua, by just using some simple mathematics. I've posted a blog post about it, so I'll direct you to there for details. But for those who don't want to read a few paragraphs, here's a couple lines of code that should help:

Code: Select all

number * 2 ^ leftShiftValue -- same as number << leftShiftValue
math.floor(number / 2 ^ rightShiftValue) -- same as number >> rightShiftValue
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Simulate Bitwise Shift Operators in Lua

Post by schme16 »

Whilst it's really interesting to know that it can be done manually like that, I've always used http://lua-users.org/wiki/BitwiseOperators for my projects.

Once again, really neat!
My Development Diary - http://shanegadsby.info
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: Simulate Bitwise Shift Operators in Lua

Post by leiradel »

I think BitOp would be a great addition to LÖVE. It is cross-platform in that it ensures consistent behavior across all supported platforms which includes Linux, MacOS X and Windows.

And it's free software under the same license as Lua!
User avatar
slime
Solid Snayke
Posts: 3143
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Simulate Bitwise Shift Operators in Lua

Post by slime »

Indeed, although Lua 5.2 will include a bitwise library AFAIK, and LuaJIT has one as well.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Simulate Bitwise Shift Operators in Lua

Post by TechnoCat »

slime wrote:Lua 5.2 will include a bitwise library AFAIK, and LuaJIT has one as well.
*Drools* :o
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: Simulate Bitwise Shift Operators in Lua

Post by leiradel »

slime wrote:Indeed, although Lua 5.2 will include a bitwise library AFAIK, and LuaJIT has one as well.
Mike Pall, author of both LuaJIT and BitOp, has publicly criticized the bitwise ops library that will ship with Lua 5.2 because it does not guarantee consistent results across platforms, because bitwise ops has undefined semantics in C.

One of the problems, if I remember it correctly, is with shift operators. Assuming a 32-bit wide word, 1 << 32 could result in:
  • 0 if the right operand is maintained as 32
  • 1 if the right operand is truncated to stay in the 0-31 range (32 & 31 = 0, 1 << 0 = 1)
Sometimes the shift implementation in the processor will use only the required bits (5 in this example) to perform the shift, so the second operand is truncated.

Mike's BitOp library does a number of checks and ensures consistent behavior. As an example, it explicitly truncates the second operand so all platforms will evaluate 1 << 32 as 1.

Cheers,

Andre
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Simulate Bitwise Shift Operators in Lua

Post by BlackBulletIV »

schme16 wrote:Whilst it's really interesting to know that it can be done manually like that, I've always used http://lua-users.org/wiki/BitwiseOperators for my projects.

Once again, really neat!
As stated, I've found pure Lua implementations to be slow (because they use tables I think). Therefore the advantage to this is that it would a lot faster.

Thanks!
leiradel wrote:I think BitOp would be a great addition to LÖVE. It is cross-platform in that it ensures consistent behavior across all supported platforms which includes Linux, MacOS X and Windows.

And it's free software under the same license as Lua!
I second that! You do need bitwise operators from time to time.

EDIT: I created an issue for it.
User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: Simulate Bitwise Shift Operators in Lua

Post by Lap »

For now there's http://luaforge.net/projects/bit/ if you want to do it in pure lua.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Simulate Bitwise Shift Operators in Lua

Post by BlackBulletIV »

I've used that. Its performance is terrible; because it uses tables I believe.
User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: Simulate Bitwise Shift Operators in Lua

Post by Lap »

BlackBulletIV wrote:I've used that. Its performance is terrible; because it uses tables I believe.
No argument there. It's damn slow.
Post Reply

Who is online

Users browsing this forum: Google [Bot], logoliv and 5 guests