Skip to the code
cf.
+10 XP

#678 Count Enabled Flags

easy

A settings export stores 32 feature toggles in one signed 32-bit integer, mask, one bit per toggle. Negative values are normal: they just mean the toggle in bit 31 is on.

Write enabledCount(mask) that returns how many of the 32 bits are 1, reading negative numbers in two's complement (so -1 has all 32 bits on).

Example 1

Input: mask = 11
Output: 3

11 is 1011 in binary: three bits are on.

Example 2

Input: mask = -1
Output: 32

In two's complement, -1 is 32 ones.

Constraints

  • -2^31 <= mask <= 2^31 - 1
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).