2.62 Check Arithmetic Right Shift

★★★

Problem:

Write a function int_shifts_arithmetic() that yields 1 when run on a machine that uses arithmetic shifts for data type int and yields 0 otherwise. Your code should work on a machine with any word size. Test your code on several machines.

#include <stdio.h>
#include <assert.h>

int int_shifts_are_arithmetic() {
    int val = -1;
    return !(val ^ (val >> 1));
}

int main() {
    assert(int_shifts_are_arithmetic());
    return 0;
}

Last updated