> For the complete documentation index, see [llms.txt](https://valineliu.gitbook.io/deuterium-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/csapp-3e-homework-solution/2.-representing-and-manipulating-information/2.79-mul3div4.md).

# 2.79 Mul3div4

★★

**Problem**:

Write code for a function `mul3div4` that, for integer argument `x`, computes `3*x/4`. Your code should replicate the fact that the computation `3*x` can cause overflow.

Code:

```c
#include <stdio.h>
#include <assert.h>
#include <limits.h>

int mul3div4(int x) {
    x = (x << 1) + x;
    int neg_flag = x & INT_MIN;
    neg_flag && (x = x + 3);
    return x >> 2;
}

int main() {
    int x = 0xF0000000;
    assert(mul3div4(x) == x*3/4);
    return 0;
}
```
