C++ Riddle - Feb-27, 2022

What does the following code print?

image

2 Likes

the loop will run 3 times. I needed to guess that this ternary syntax means:
if a == 1 → 3 bits, otherwise → 5 bits.

so shifting to the left will be:
001
010
100
which is 1 2 4.
Initially I also mistakenly thought that even when we specify 3 bit wide, we might be running the loop for 32 times. But this is wrong. We run it for the bit width of the “a: 3”. i.e. 3 times.

1 Like

Very good Kobi.
I will not say yet if you are right, to keep the riddle alive.
But if I would need to guess I’d say you got it right! (or maybe almost right? :thinking:).

I would have the same explanation as Kobi the only difference is that I think that in the syntax
uint a : 1 ? 3 : 5 {};
1 is a the ternary statement which is true and that’s why the number of bits are 3

I have a question: How would you implement sizeof bits for a bit field in compile time?

@avilachmish I will make it our next riddle!

And it is time, by now, to announce that @kobica answer above was full and correct! :clap::clap:

1 Like