For 2021 - online Python 3 training - see ((here)).
Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7. We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.
Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc. |
Binary / bitwise operations in Lua with the standard bit32 library
If you want to do bit manipulation in Lua, you can use the bit32 library (which is loaded as a table) from Lua 5.2, and it's backported to newer Lua 5.1 versions as well. Bit manipulation is particularly useful in applications where data is packed into a series of bytes - in effect an array of booleans.
Let's see some of them in use - we have the "classic" and, or, exclusive or and not:
wizard:j graham$ lua52
Lua 5.2.0 Copyright (C) 1994-2011 Lua.org, PUC-Rio
> value = 0xffff0000
> nothr = 0x00ffff00
> = string.format("%08x",bit32.band(value,nothr))
00ff0000
> = string.format("%08x",bit32.bor(value,nothr))
ffffff00
> = string.format("%08x",bit32.bxor(value,nothr))
ff00ff00
> = string.format("%08x",bit32.bnot(nothr))
ff0000ff
We have shifts and rotates:
• lshift - left shift
• alshift - arithmetic left shift; special consideration for sign bit
• lrotate - left shift, rotating bits that fall off the end
and the equivalent right shifts too.
There's an example of some of these in use in a program [here] - combining bitwise operators with other Lua features.
The bit32 library also includes operators to extract and even replace bits - illustrated below using hex values to make it clearer to see what is happening, but you may in practise choose any boundaries.
Lua 5.2.0 Copyright (C) 1994-2011 Lua.org, PUC-Rio
> value = 0xfe84c372
> = bit32.extract(value,8,4)
3
> = bit32.extract(value,8,8)
> = string.format("%x",bit32.extract(value,8,8))
c3
> = string.format("%x",bit32.replace(value,0x95,12,8))
fe895372 (written 2012-04-06)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles U198 - New in Lua 5.2 [3683] Weak references in Lua - what are they, and why use them? - (2012-04-04) [3686] The goto statement in Lua - (2012-04-06) [3690] Changes in Lua 5.2 - Garbage Collection - (2012-04-08) U108 - Lua - Pattern matching [1744] Lua examples, Lua Courses - (2008-08-08) [1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18) [2383] Lua Regular Expressions - (2009-08-28) [2702] First and last match with Regular Expressions - (2010-04-02) [2727] Making a Lua program run more than 10 times faster - (2010-04-16) [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
Some other Articles
Back in Cambridge to give a Lua courseCan I cycle or ride my mobility scooter on the pavement?Shopkeepers take an Easter break tooBinary / bitwise operations in Lua with the standard bit32 libraryProgramming Standards in LuaOnce upon a Maundy ThursdayMelksham Business Newsreel
|
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|