Challenge 1: Count Set Bits

This is a traditional problem counting the number of set bits or 1's present in a number.

Introduction

Let’s see how we can count the set bits using the AND operator.

What are set-bits?

1 = set-bit

0 = unset-bit

For example:

Input: 5

Output: 0101 (in binary)

There are two set bits are two in the above example, as we have two 1’s in the binary representation.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy