Overview

A binary digit or bit is a 0 or 1 character. A bit string is then a contiguous sequence of bits. It’s weight is a reference to the number of 1s in the bit string. Compare the below operation to the method for converting from one numerical base to another (e.g. hexadecimal).

unsigned int bit_weight(int64_t n) {
  unsigned int count = 0;
  while (n) {
    count += (n % 2 == 0) ? 0 : 1;
    n /= 2;
  }
  return count;
}

1 item under this folder.