Skip to content Skip to sidebar Skip to footer

Why Exponential Enum Keys Are Used Here?

Here you can find this code: enum NodeFlags { None = 0, Let = 1, Const = 2, NestedNamespace = 4, Synthesized = 8, Namespace = 16, ExportContext = 32,

Solution 1:

The reason is that this enum is used as a flags enum. These values can be combined using the bitwise or (|) operator. So you can have a value that is simultaneously both memebers of the enum

let x = NodeFlags.AwaitContext  | NodeFlags.ThisNodeHasError // The node is both an await context but also has errors

For this to work the values must not interfere with each other at a bit level, so each value must be a power of two (which will only have a single bit set to one at a different position for each power)

Basically each bit in the value is an independent flag. They could have gone with separate flags on the node such as isLet, isConst, isAwaitContext, isError etc, but that would have been wasteful in terms of memory and for a compiler that adds up since there are a lot of nodes. Like this a single field of 64 bits can represent 64 flags.

To extract which value is set you can use the & operator for example x & NodeFlags.ThisNodeHasError !== 0 would mean the ThisNodeHasError is set in the x variable.

Solution 2:

With a combined flag, you could get the enum types by checking the values with a bitwise AND &.

This works in the other direction as well where you could just add all flags with bitwise OR |

constgetFlags = value => Object.keys(nodeFlags).filter(k => nodeFlags[k] & value);

var nodeFlags = { None: 0, Let: 1, Const: 2, NestedNamespace: 4, Synthesized: 8, Namespace: 16, ExportContext: 32, ContainsThis: 64 },
    blockScoped = 3,
    flagsOfBlockScoped = getFlags(blockScoped);
    
console.log(flagsOfBlockScoped);

Solution 3:

These are all powers of two, so in binary, you'd have

None = 0b0,
Let = 0b1,
Const = 0b10,
NestedNamespace = 0b100,
Synthesized = 0b1000,
Namespace = 0b10000,

And so on. This makes it possible to combine the flags, for example 111 means Let, Const, NestedNamespace

In your case ReachabilityCheckFlags = 384 is 0b110000000 in binary, so it combines the flags with values 128 (0b10000000) and 256 (0b100000000)

Post a Comment for "Why Exponential Enum Keys Are Used Here?"