---
# System prepended metadata

title: 11847 - Prefix Boolean expression
tags: [prefix]

---

# 11847 - Prefix Boolean expression
>author: Utin
###### tags: `prefix`

---

## Brief
用全域變數idx模擬scanf的操作，並用0~15二進制作bitwise comparison遍歷所有條件

## Solution 0

```c=
#include <stdio.h>

char arr[4], expr[31], idx;

char op();

int main() {
    scanf("%s", expr);
    for (int i = 0; i < 16; i++) {

        // initialize
        idx = 0;

        for (int j = 0; j < 4; j++) {

            // bitwise comparison
            if ((1<<(3-j)) & i) arr[j] = 1;
            else arr[j] = 0;

            // output the value of A, B, C, D
            printf("%d ", arr[j]);
        }

        // output the result
        printf("%d\n", op());
    }
}

char op() {
    char c = expr[idx++];
    if (c == '|') return op() | op();
    if (c == '&') return op() & op();
    return arr[c - 'A'];
}

// By Utin
```

## Reference