Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
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
U108 - Lua - Pattern matching
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
  [2727] Making a Lua program run more than 10 times faster - (2010-04-16)
  [2702] First and last match with Regular Expressions - (2010-04-02)
  [2383] Lua Regular Expressions - (2009-08-28)
  [1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18)
  [1744] Lua examples, Lua Courses - (2008-08-08)

U198 - New in Lua 5.2
  [3690] Changes in Lua 5.2 - Garbage Collection - (2012-04-08)
  [3686] The goto statement in Lua - (2012-04-06)
  [3683] Weak references in Lua - what are they, and why use them? - (2012-04-04)


Back to
The goto statement in Lua
Previous and next
or
Horse's mouth home
Forward to
Shopkeepers take an Easter break too
Some other Articles
Back in Cambridge to give a Lua course
Can I cycle or ride my mobility scooter on the pavement?
Shopkeepers take an Easter break too
Binary / bitwise operations in Lua with the standard bit32 library
Programming Standards in Lua
Once upon a Maundy Thursday
Melksham Business Newsreel
4755 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3687_Bin ... brary.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat