DEV Community

Discussion on: Bitmasks: A very esoteric (and impractical) way of managing booleans

Collapse
 
moopet profile image
Ben Sinclair

Permissions are the only real way most people will see bitmasks these days unless you're doing something particularly low-level.
Like file permissions:

rwxr-xr-x

reads literally as

1111101101 (i.e. 755)

Not realising this is exactly why everyone abuses chmod -R 777 so wildly.

It's also good1 for wrapping up complex permissions in a program:

function isLivingRelative(person) {
  return person.isAlive && person.isRelative;
}

function canBorrowMyCar(person) {
  return isLivingRelative(person) || person.wasQuickWithTheLawmowerThatOneTime;
}

vs.

const isAlive = 1;
const isRelative = 2;
const wasQuickWithTheLawmowerThatOneTime = 4;
const ISLIVINGRELATIVE = isAlive | isRelative;
const CANBORROWMYCAR = ISLIVINGRELATIVE | wasQuickWithTheLawmowerThatOneTime;

  1. Meh, "good". 

Collapse
 
somedood profile image
Basti Ortiz

I was supposed to include an example on complex conditions in the article, but then I thought that it was probably getting too long already. Thanks for bringing this up!

Also, may I ask what the xs stand for in rwxr-xr-x?

Collapse
 
moopet profile image
Ben Sinclair

They're the execute bits - if it's a file, you can run it as a command, if it's a directory, you can cd into it.