← All problems · Bit Manipulation
Clear Lowest Pending Event
Clear the lowest set bit of a pending-event mask.
Implement uint32_t clear_low(uint32_t x).
Choose C or C++ and write your code on the right and press Run (or Ctrl + Enter). Hidden tests check your functions and print the result below the editor.
Subtract one and AND.
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
uint32_t clear_low(uint32_t x)
{
return x & (x-1u);
}
#include <cstdio>
#include <cstdint>
#include <cstddef>
#include <cstring>
std::uint32_t clear_low(std::uint32_t x)
{
return x & (x-1u);
}
Subtraction flips the trailing zeros and lowest one.
solution.c
OutputPress Run to compile and test your code.
Show the hidden tests
/* ---- tests ---- */
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
int main(void)
{
CHECK(clear_low(0)==0);
CHECK(clear_low(12)==8);
CHECK(clear_low(1)==0);
puts(fails ? "Some tests failed" : "All tests passed");
return fails != 0;
}