Why does this bitwise operation return 30 instead of 384?

I am using the Dev-C ++ compiler. This program is supposed to print 30, but print it 384.

#include <stdio.h>

int main() {
    int n = 3;
    int ans;

    ans = n<<3 + n<<1;
    printf("%d", ans);

    getch();
    return 0;
}
+4
source share
1 answer

The problem is that the operator +has a higher priority than the operator <<. In fact, you wrote:

ans = n << (3 + n) << 1;

Actually you want:

ans = (n<<3) + (n<<1);
+9
source

Source: https://habr.com/ru/post/1544757/


All Articles