2

How do you know if a particular bit is set in dart?

example how to do this?

int value = 5;

if(checkBit(value, 4)) {
    print("position: 4 TRUE");
} else {
    print("position: 4 FALSE");
}
julemand101
  • 28,470
  • 5
  • 52
  • 48
Adam Nowak
  • 41
  • 3

1 Answers1

4

The solution is the same as @MihaiBC points to but since that is in C#, I am still going to post this just so we also have a Dart example:

void main() {
  const value = 5;

  print(value.toRadixString(2).padLeft(64, '0'));
  // 0000000000000000000000000000000000000000000000000000000000000101
  
  print(checkBit(value, 0)); // true
  print(checkBit(value, 1)); // false
  print(checkBit(value, 2)); // true
  print(checkBit(value, 3)); // false
}

bool checkBit(int value, int bit) => (value & (1 << bit)) != 0;
julemand101
  • 28,470
  • 5
  • 52
  • 48