</>EmbeddedInterview
← All problems · Drivers & Peripherals

Acknowledge Write-One-to-Clear Flags

Easy

Return the value to write to a simulated write-one-to-clear status register, acknowledging only handled pending bits. Implement uint32_t ack_value(uint32_t pending, uint32_t handled).

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.

Intersect pending and handled masks.

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>

uint32_t ack_value(uint32_t pending, uint32_t handled)
{
    return pending & handled;
}

Writing one clears that bit; do not use a read-modify-write on a W1C register.

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(ack_value(15,5)==5);
    CHECK(ack_value(2,1)==0);
    CHECK(ack_value(0,UINT32_MAX)==0);
    puts(fails ? "Some tests failed" : "All tests passed");
    return fails != 0;
}