Casting binary to bit
<p>Based on the <a href="https://stackoverflow.com/q/36616240/2523686">recent</a> question.</p>

<p>Can someone point me to explanation of the following?</p>

<p>If I cast <code>binary(4)</code> constant <code>0x80000000</code> to <code>int</code>, take the resulting value and cast it to <code>bit</code> type, the result is 1.</p>

<pre><code>select cast(0x80000000 as int) --&gt; -2147483648
select cast(-2147483648 as bit) --&gt; 1
</code></pre>

<p>But if I cast <code>0x80000000</code> to bit type directly the result is 0.</p>

<pre><code>select cast(0x80000000 as bit) --&gt; 0
</code></pre>

<p>I hoped to get 1 in this case as well, thinkning that probably this expression equivalent to</p>

<pre><code>select cast(cast(0x80000000 as binary(1)) as bit)
</code></pre>

<p>but this is not the case. Instead, it seems that the highest byte of the binary constant is taken and converted to bit. So, effectively it is something like</p>

<pre><code>select cast(cast(right(0x80000000, 1) as binary(1)) as bit)
</code></pre>

<p>I'm clear with first <code>binary -&gt; int -&gt; bit</code> part. What I'm not clear with is the second <code>binary -&gt; bit</code> part. I was not able to find this behavior explained in the <a href="https://msdn.microsoft.com/en-us/library/ms177603.aspx" rel="nofollow noreferrer">documentation</a>, where only</p>

<blockquote>
  <p>Converting to bit promotes any nonzero value to 1.</p>
</blockquote>

<p>is stated.</p>