Reading and Rewriting a Flags Value

Easy

Read one whole number from standard input into an unsigned int named flags, then report five facts about its bits without changing the variable itself. Print scanf's conversion count first, as the previous exercises did, then the value in decimal and hexadecimal, then bit 0 and bit 3 extracted as plain 0 or 1 with the (flags >> n) & 1u idiom, then the value with bit 3 set using |, then the value with bit 1 cleared using & ~. Build both masks with 1u << n rather than typing hex constants, and note where the u suffix goes and why: everything here is unsigned, which is what keeps the shifts clear of the undefined and implementation-defined cases signed shifts have. Print every hexadecimal value with %#x so the 0x prefix comes along. Watch the 255 test case, where setting a bit that is already set leaves the value alone, which is exactly what | promises.

Success Criteria

Your code must pass 3 test case(s) to complete this exercise. 3 hint(s) are available if you need help.

Sign in to track your progress

You can work on exercises as a guest, but sign in to track your progress and save your submissions.

This platform is built by its community

Every lesson, project, and tool on HelloC++ is funded by sponsors. Join them and help shape what we build next.

Become a Patron