# New exercise suggestion: Pop Count

This might be a simple exercise to add for Nibbly November, as one way to avoid repeating any exercises from other months.

pop-count
Pop Count

Count the number of 1 bits in the binary representation of the value.

Keep your hands off that bit_count provided by your standard library!

Suggested canonical test cases:
0
1
9
70
2_000_000_000

The concept is well known:

Wikipedia: Hamming weight

GCC: `int` __builtin_popcount `(unsigned int x)`
Returns the number of 1-bits in `x`.

Java: `public static int` bitCount`(int i)`
Returns the number of one-bits in the two’s complement binary representation of the specified int value. This function is sometimes referred to as the population count.

Haskell: popCount1 `:: v -> Count`
The number of 1-bits in the value.

Python: `int.`bit_count`()`
Return the number of ones in the binary representation of the absolute value of the integer. This is also known as the population count.

x86: POPCNT
This instruction calculates the number of bits set to 1.

I believe @kytrinyx is asking that any new exercises be accompanied but a relatable story that makes the exercise more about solving something real world related and not purely programming oriented. You may want to add that to the proposal

Proposed story:

A checkers program uses a binary number to remember which squares are occupied.

The 1’s bit is set if the first square is occupied, the 2’s bit is set if the second square is occupied, the 4’s bit is set if the third square is occupied, and so on.

This representation is called a bitset.

Given the number, how many squares are occupied?

That’s quite programming-oriented It has “program” in the first three words!

## Eliud’s Eggs

Your friend Eliud inherited a farm from her grandma Tigist. Her granny was an inventor and had a tendency to build things in an overly complicated manner. The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up.

Eliudis is asking you to write a program that shows the actual number of eggs in the coop.

The position information encoding is calculated as follows:

1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
2. Convert the number from binary to decimal
3. Show the result on the display

Example 1:

``````Chicken Coop:
_ _ _ _ _ _ _
|E| |E|E| | |E|

Resulting Binary:
1 0 1 1 0 0 1

Decimal number on the display:
89

Actual eggs in the coop:
4
``````

Example 2:

``````Chicken Coop:
_ _ _ _ _ _ _ _
| | | |E| | | | |

Resulting Binary:
0 0 0 1 0 0 0 0

Decimal number on the display:
16

Actual eggs in the coop:
1

``````
6 Likes

(automatically closed) problem-specifications PR

(automatically closed) website-icons issue

@iHiD is on vacation. It might be worth waiting for him to weigh in prior to proceeding here.

I’ve reviewed the PR! Great work.

Nice. Thanks

I have a PR for the C++ track with the exercise.

I wonder if I should write an append to discourage the “easy” way with bitset’s count? What do you think @siebenschlaefer?

Keep your hands off that bit-count functionality provided by your standard library!

There are several methods, that make this an easy one-liner. That’s why I wanted to drive the point a bit further.

We still need an icon.

True. CC @ihid

I’ve tried to create an instructional graphic:

I could use some help converting the scalable PDF to SVG as all the converters I’ve tried are breaking the eggs.

Thanks !

Maybe this will fare better if the egg is more of a cartoon style? Just a black oval shape outline and white inside similar to this: Egg icon PNG and SVG Vector Free Download ?

Thanks @glaxxie :

1 Like

I like the art style. If we use an image, we should supply a good alt-text. I have no idea how the current “ascii art” fares with screen readers. Are there people here who can comment on that?

The exercise icons are 160x160 svg.

Here’s an example by a non-artist:

As for serving alt text with exercise icons, I think that would be a website change proposal, affecting all exercises. Perhaps start a separate forum thread.

I had some minor confusion related to this.
I’m plodding through WASM for November and saw I need to solve “pop-count” to get the badge but didn’t see that exercise listed. Discovered this thread and see the exercise has the new name “Eliud’s Eggs” while the pop-up text when I hover over the badge reads “Solve pop-count in …”. Same goes for the descriptive text in the Nibbly November post published at The #12in23 Challenge - still refers to Pop Count and not the new name.