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
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: Simulate Bitwise Shift Operators in Lua

Post by ishkabible »

bit shifts are easy in Lua, it's bitwise and, or, and not that are tough.

to get x=y<<by you simply use

Code: Select all

x=y*2^by
to get x=y>>by you simply use

Code: Select all

x=floor(y/2^by)
simple and fast
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 »

Lol. From the first post:
BlackBulletIV wrote: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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests