title: Portable BIT macro in C
body: <p>I have a bare metal SW environment that can and compile run on multiple architectures such as <code>ARM 32-bit v7</code>/<code>ARM 64-bit v8</code> and a custom in-house architecture: let's call it <code>ARCHX</code>:</p>
  
  <p>First how different data types are defined on <code>ARM32</code>, <code>ARM64</code> and <code>ARCHX</code>:</p>
  
  <p>(Note: <code>ARM64</code> uses <code>LP64</code> programming model in this example).</p>
  
  <ul>
  <li>ARM32/ARM64/ARCHX
  
  <ul>
  <li>int: 32/32/16</li>
  <li>long: 32/64/32</li>
  <li>pointer: 32/64/16</li>
  </ul></li>
  </ul>
  
  <p>I've implemented a BIT-macro:</p>
  
  <pre><code>#define BIT(n) (0x1U &lt;&lt; (n))
  </code></pre>
  
  <p>Now on <code>ARCHX</code> if using <code>BIT</code> on an int (16-bit in <code>ARCHX</code>) this gives an error:</p>
  
  <blockquote>
    <p>Warning: shiftcount too large, result is undefined</p>
  </blockquote>
  
  <p>Now adding <code>UL</code> to this macro solves that problem but in general might cause problems on other architectures.</p>
  
  <p>So my question is: </p>
  
  <p>Is this definition of the <code>BIT</code> macro not portable between architectures and compilers since I need to use <code>UL</code> on some archs, <code>U</code> on some and perhaps even <code>ULL</code> on some combinations (<code>ARM64</code> and <code>LLP</code> programming model?). </p>
  
  <p>Perhaps the <code>BIT</code> macro should be renamed and divided into <code>BIT16</code>, <code>BIT32</code> &amp; <code>BIT64</code> and be implemented per architecture rather than a generic <code>BIT</code>-macro for all architectures? </p>