Skip to content

Instantly share code, notes, and snippets.

@beiweiqiang
Last active June 21, 2018 00:28
Show Gist options
  • Save beiweiqiang/de19c182a48a52b6920661741924a6fd to your computer and use it in GitHub Desktop.
Save beiweiqiang/de19c182a48a52b6920661741924a6fd to your computer and use it in GitHub Desktop.
如果机器是对 int 类型数使用算术右移, 返回 1, 其他返回 0
#include <stdio.h>
/*
* CSAPP exercise 2.62
* */
/*
* 如果机器是对 int 类型数使用算术右移, 返回 1, 其他返回 0
* */
int int_shifts_are_arithmetic();
/*
* 正数返回 1, 负数返回 0
* */
int is_positive(int x);
int main() {
printf("%d \n", int_shifts_are_arithmetic());
return 0;
}
int int_shifts_are_arithmetic() {
int shift = (sizeof(int) << 3) - 1;
return !is_positive(((1 << shift) >> 1));
}
int is_positive(int x) {
int shift = (sizeof(int) << 3) - 1;
return !((x >> shift) & 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment