-8
   hour = (bufm[1] &= ~((1 << 7) | (1 << 6) | (1 << 5)));

I did not understand what this code does and therefore I cannot translate it into dart. I see that there are bitwise shifts

  • This is also valid Dart code. – AKX Mar 21 '23 at 12:43
  • `<<` is the "shift left" operator. `|` is the bitwise OR operator, `&` is the bitwise AND operator and `~` is the bitwise NOT operator. Any C book or documentation explains this. For example https://cplusplus.com/doc/tutorial/operators/ – Jabberwocky Mar 21 '23 at 12:46
  • Does it help if you split it into two statements? `hour = bufm[1] & ~((1 << 7) | (1 << 6) | (1 << 5)); bufm[1] = hour;` – zwol Mar 21 '23 at 12:47
  • Hint (in case you haven't found out by yourself in the meantime) : `1 << x` is equivalent to "2 raised to the power of x". – Jabberwocky Mar 21 '23 at 12:49
  • Short answer (assuming `bufm[1]` and `hour` are unsigned, 8-bit types), the code clears the top 3 bits of the former and assigns that value to the latter. – Adrian Mole Mar 21 '23 at 12:49

1 Answers1

0

&= means take only the bits that are set to 1 in both operands

~ reverses all bits,

<< moves the bits to the left n times,

| combines bits from operands

so

hour = (bufm[1] &= ~((1 << 7) | (1 << 6) | (1 << 5))); // 128, 64, 32
hour = (bufm[1] &= ~(128 | 64 | 32)); // ~0x000000E0, reverse bits
hour = (bufm[1] &= 0xFFFFFF1F); // keep only those bits, ie. remove bits 5 6 7

I assume hour is an int (32 bits). It removes bit 5, 6 and 7 from bufm[1] and stores it in hour.

AR7CORE
  • 268
  • 1
  • 8