Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/3687_Bin ... brary.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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)


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
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).

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
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: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb