× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Set the i-th bit to zero?

Ray Phan
Feb 02, 2015
<p>You just have to replace the logical <code>OR</code> with a logical <code>AND</code> operation. You would use the <code>&amp;</code> operator for that:</p> <pre><code>pt = pt &amp; ~(1 &lt;&lt; i); </code></pre> <p>You have to <strong>invert</strong> your mask because logical <code>AND</code>ing with a <code>1</code> will maintain the bit while <code>0</code> will clear it... so you'd need to specify a <code>0</code> in the location that you want to clear. Specifically, doing <code>1 &lt;&lt; i</code> will give you a mask that is <code>000...010..000</code> where the <code>1</code> is in the bit position that you want, and inverting this will give <code>111...101...111</code>. Logical <code>AND</code>ing with this will clear the bit that you want.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/26004263/Set%20the%20i-th%20bit%20to%20zero?/26004296">Stack Overflow</a>.</p>
comments powered by Disqus