title: How to detect in C whether your machine is 32-bits
body: <p>So I am revising for an exam and I got stuck in this problem:</p>
  
  <hr>
  
  <p>2.67 ◆◆
  You are given the task of writing a procedure int_size_is_32() that yields 1
  when run on a machine for which an int is 32 bits, and yields 0 otherwise. You are
  not allowed to use the sizeof operator. Here is a first attempt:</p>
  
  <pre><code>1 /* The following code does not run properly on some machines */
  2 int bad_int_size_is_32() {
  3 /* Set most significant bit (msb) of 32-bit machine */
  4 int set_msb = 1 &lt;&lt; 31;
  5 /* Shift past msb of 32-bit word */
  6 int beyond_msb = 1 &lt;&lt; 32;
  7
  8 /* set_msb is nonzero when word size &gt;= 32
  9 beyond_msb is zero when word size &lt;= 32 */
  10 return set_msb &amp;&amp; !beyond_msb;
  11 }
  </code></pre>
  
  <p>When compiled and run on a 32-bitSUNSPARC, however, this procedure returns 0. The following compiler message gives us an indication of the problem: warning: left shift count >= width of type</p>
  
  <p>A. In what way does our code fail to comply with the C standard?</p>
  
  <p>B. Modify the code to run properly on any machine for which data type int is
  at least 32 bits.</p>
  
  <p>C. Modify the code to run properly on any machine for which data type int is
  at least 16 bits.</p>
  
  <p><em><strong></em>__<em>_</em>__<em>_</em>____</strong> MY ANSWERS:</p>
  
  <p>A: When we shift by 31 in line 4, we overflow, bec according to the unsigned integer standard, the maximum unsigned integer we can represent is 2^31-1</p>
  
  <p>B: In line 4 1&lt;&lt;30</p>
  
  <p>C: In line 4 1&lt;&lt;14 and in line 6 1&lt;&lt;16</p>
  
  <p>Am I right? And if not why please? Thank you!</p>
  
  <p><em><strong></em>__<em>_</em>__<em>_</em>____</strong> Second tentative answer:</p>
  
  <p>B: In line 4 (1&lt;&lt;31)>>1 and in line 6: int beyond_msb = set_msb+1; I think I might be right this time :)</p>