I wanted to write about bit-masks and bitwise operators for a while now as a reference for myself and to deepen my understanding of the topic. But I couldn’t finish it, because frankly it was just too boring.
I do a lot of Android programming at work recently and stumbled upon a class called
SparseBooleanArray that was used in one tutorial to store selection states for items in a table. The documentation says it’s a more memory efficient way to store an array of boolean values than to use for example an
ArrayList. And I thought I can make an interesting post by writing a similar thing in Swift using bit-masks.
To store a
Bool in Swift and in practically all other languages you have to allocate one byte, which is usually the least amount of memory you can request from the system. Which is unfortunately 8