jed_fox1’s avatarjed_fox1’s Twitter Archive — № 717

      1. …in reply to @JoshWComeau
        @JoshWComeau @MrMartineau One use case for this is having multiple flags passed as an argument. This isn’t as common in JS since we have objects and arrays, but for example the open system call in C (man7.org/linux/man-pages/man2/open.2.html) takes a flags argument. You can have your function take up to 64 different…
    1. …in reply to @jed_fox1
      @JoshWComeau @MrMartineau …flags, where each flag has a value containing a power of two. That means the binary representations of these flags consist of 1 one and 63 zeroes. You can combine the flags using a bitwise | (for example, (2**1) | (2**2) sets the last two bits to one and the rest to 0). Then…
  1. …in reply to @jed_fox1
    @JoshWComeau @MrMartineau …you can check if a flag is set by doing flagsArg & flagToCheck, which will set all the bits to zero except the one corresponding to the flag. If the result of that is non-zero, the flag was set.