JavaScript How to use bitmasks to store boolean values in JavaScript

I'm a bit confused after reading about how to use bitmasks to store boolean values. I would like to have a set of boolean values and then generate a unique integer for each combination of the values. Here is the current system so far:

var a = 1 << 1
var b = 1 << 2
var c = 1 << 3
var d = 1 << 4

var uniqueint1 = getInt(true, false, false)
var uniqueint2 = getInt(true, true, false)
var uniqueint3 = getInt(true, true, true)
// ...

function getInt(x, y, z) {
  var value = a
  if (x) value = value | b
  if (y) value = value | c
  if (z) value = value | d
  return value
}

But the problem is, I am not sure if I am supposed to handle the "not" case, along the lines of this:

function getInt(x, y, z) {
  var value = a
  if (x) value = value | b
  else value = value ^ b

  if (y) value = value | c
  else value = value ^ c

  if (z) value = value | d
  else value = value ^ z

  return value
}

All I know is that I've seen |, &, and ^ related to bitmasks, and I know to find if a boolean is in the bitmask you do var yes = value & b, but I'm just confused how to generate the bitmask so that it handles both the if (true) and if (false) cases. Not sure if I'm supposed to be using ^ somewhere, or what else I'm doing wrong. Please advise. Thank you!

Answer:1

I am not sure if I am supposed to handle the "not" case

You're not supposed to handle it at all. A bitmask works so that you either have a bit with value 1 if your flag is set or the bit with value 0 if not. Given that your value starts with 0s everywhere (the integer 0), you just have to set the bit (by OR 1) when your condition is true and you don't have to do anything otherwise.

You could simplify your code to

function getInt(x, y, z) {
  return (0b10 // a
          | (x << 2)   // 0b00100 if x and 0b00000 else
          | (y << 3)   // 0b01000 if y and 0b00000 else
          | (z << 4)); // 0b10000 if z and 0b00000 else
}
Answer:2

I am not able to show two bar charts in one page. I have tried both ways, implementing these in two different <script> sections in and in one <script> section also. If one is shown if I ...

I am not able to show two bar charts in one page. I have tried both ways, implementing these in two different <script> sections in and in one <script> section also. If one is shown if I ...

  1. google charts work offline
  2. google charts not working
  3. google charts not working in ie11
  4. google charts not working in safari
  5. google charts stopped working
  6. google charts not working in chrome
  7. google charts not working in firefox
  8. google charts not working in internet explorer
  9. google charts not working in ie
  10. google charts not working in edge
  11. does google charts work offline
  12. google.charts.setonloadcallback not working
  13. google charts animation not working
  14. google.charts.load not working
  15. google charts colors not working
  16. google charts chartarea not working
  17. google charts options not working
  18. google charts ticks not working
  19. google charts isstacked not working
  20. google charts interpolatenulls not working

Why is the text of the paragraph always one letter behind? Shouldn't it automatically "copy" the value of the input to the paragraph and always be the same text? How can I fix this problem? $(...

Why is the text of the paragraph always one letter behind? Shouldn't it automatically "copy" the value of the input to the paragraph and always be the same text? How can I fix this problem? $(...

  1. input equal output
  2. input equal output energy
  3. should input equal output
  4. does work input equal output
  5. why does input equal output

When i click on a element with a class named sections, i should get an alert showing what is it's class index, but it's showing a wrong number. For example, if i click the first div element with a ...

When i click on a element with a class named sections, i should get an alert showing what is it's class index, but it's showing a wrong number. For example, if i click the first div element with a ...

  1. jquery indexof
  2. jquery indexof array
  3. jquery indexof is not a function
  4. jquery indexof case insensitive
  5. jquery index not working
  6. jquery indexof not working
  7. jquery index method
  8. jquery indexof multiple values
  9. jquery index returns negative
  10. jquery index of selected option
  11. jquery index of clicked element
  12. jquery indexeddb
  13. jquery index of class
  14. jquery indexof array of objects
  15. jquery index of parent
  16. jquery index greater than
  17. jquery index each
  18. jquery indexof return value
  19. jquery indexeddb plugin
  20. jquery index start at 1

How can I get the class name of the parent div from which a html link calls a javascript function? In the following code example: <div class="parent-class"> <a href="javascript:parent(...

How can I get the class name of the parent div from which a html link calls a javascript function? In the following code example: <div class="parent-class"> <a href="javascript:parent(...

  1. get class name parent jquery
  2. css class name parent
  3. jquery class name parent
  4. python class parent name
  5. ruby class parent name
  6. python get class parent name
  7. parent class name javascript
  8. parent class name
  9. php get class parent name
  10. parent class name php
  11. get class name of parent
  12. get class name of parent div jquery
  13. parent class name c++