2.67

A.

In section 6.5.7 Bitwise shift operators of c11 standard, it said

If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior it undefined.

B.

check function int_size_is_32

C.

check function int_size_is_32_for_16bit

/*
 * int-size-is-32.c
 */

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

/* The following function does not run properly on some machine */
/*
int bad_int_size_is_32() {
  int set_msb = 1 << 31;
  int beyond_msb = 1 << 32;

  return set_msb && !beyond_msb;
}
*/

int int_size_is_32() {
  int set_msb = 1 << 31;
  int beyond_msb = set_msb << 1;

  return set_msb && !beyond_msb;
}

int int_size_is_32_for_16bit() {
  int set_msb = 1 << 15 << 15 << 1;
  int beyond_msb = set_msb << 1;

  return set_msb && !beyond_msb;
}

int main(int argc, char *argv[]) {
  assert(int_size_is_32());
  assert(int_size_is_32_for_16bit());
  return 0;
}
comments powered by Disqus