<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Bit field - Page 14 - Wikipedia">
<p>
<a accesskey="1" href="page.php?w=Bit_field&amp;p=13">1.Previous</a><br />
<a accesskey="3" href="page.php?w=Bit_field&amp;p=15">3.Next</a>
</p>
<p>& Self::OPAQUE != 0    }</p>

<p>    fn set_opaque(&mut self, v: bool) {        self.raw[0] = (self.raw[0] & !Self::OPAQUE) | (v as u8);    }</p>

<p>    fn fill_color(&self) -> u8 {        (self.raw[0] & Self::FILL_COLOR_MASK) >> 1    }</p>

<p>    fn set_fill_color(&mut self, v: u8) {        self.raw[0] =            (self.raw[0] & !Self::FILL_COLOR_MASK) | ((v & 0b111) << 1);    }</p>

<p>    fn width(&self) -> u8 {        self.raw[2] & Self::WIDTH_MASK    }</p>

<p>    fn height(&self) -> u8 {        (self.raw[2] & Self::HEIGHT_MASK) >> 4    }}</p>

<p><big>Processor status register</big></p>
<p>The <a href="page.php?w=status_register">status register</a> of a processor is a bit field consisting of several flag bits. Each flag bit describes information about the processor's current state. As an example, the status register of the <a href="page.php?w=MOS_Technology_6502">6502</a> processor is shown below:</p>

<p>These bits are set by the processor following the result of an operation. Certain bits (such as the Carry, Interrupt-disable, and Decimal flags) may be explicitly controlled using set and clear instructions. Additionally, branching instructions are also defined to alter execution based on the current state of a flag.</p>

<p>For an instance, after an <code>ADC</code> (Add with Carry) instruction, the <code>BVS</code> (Branch on oVerflow Set) instruction may be used to jump based on whether the <a href="page.php?w=overflow_flag">overflow flag</a> was set by the processor following the result of the addition instruction.</p>

<p><big>Extracting bits from flag words</big></p>
<p>A subset of flags in a flag field may be extracted by <a href="page.php?w=Logical_conjunction">AND</a>ing with a <a href="page.php?w=Mask_%28computing%29">mask</a>. A large number of languages support the <a href="page.php?w=logical_shift">shift</a> operator (&lt;&lt;) where <code>1 << n</code> aligns a single bit to the nth position. Most also support the use of the <a href="page.php?w=Logical_conjunction">AND</a> operator (&) to isolate the value of one or more bits.</<></code></p>

<p>If the status-byte from a device is 0x67 and the 5th flag bit indicates data-ready. The mask-byte is <code>2^5 = 0x20</code>. <a href="page.php?w=Logical_conjunction">AND</a>ing the status-byte 0x67 (<code>0110&nbsp;0111</code> in binary) with the mask-byte 0x20(<code>0010&nbsp;0000</code> in binary) evaluates to 0x20. This means the flag bit is set i.e., the device has data ready. If the flag-bit had not been set, this would have evaluated to 0 i.e., there is no data available from the device.</p>

<p>To check the <b>n</b>th bit from a variable <b>v</b>, perform either of the following: (both are equivalent)<syntaxhighlight lang="c">bool nth_is_set = (v & (1 << n)) != 0;bool nth_is_set = (v >> n) & 1;</<></syntaxhighlight></p>

<p><big>Changing bits in flag words</big></p>
<p>Writing, reading or toggling bits in flags can be done only using the OR, AND and NOT operations - operations which can be performed quickly in the processor. To set a bit, <a href="page.php?w=Logical_operator">OR</a> the status byte with a mask byte. Any bits set in the mask byte or the status byte will be set in the result.</p>

<p>To toggle a bit, <a href="page.php?w=Exclusive_disjunction">XOR</a> the status byte and the mask byte. This will set a bit if it is cleared or clear a bit if it is set.</p>

<p><big>See also</big></p>
<p>
* <br/>
* <br/>
* <br/>
* <br/>
* <br/>
* <br/>
* <br/>
* <br/>
* <br/>
* <br/>
* </p>

<p><big>Notes</big></p>
<p><big>References</big></p>
<p><big>External links</big></p>
<p>
* <br/>
* <br/>
* <br/>
*  ()<br/>
* : Several snippets of C code manipulating bit fields </p>

<p></p>
<p>
<a accesskey="1" href="page.php?w=Bit_field&amp;p=13">1.Previous</a><br />
<a accesskey="3" href="page.php?w=Bit_field&amp;p=15">3.Next</a>
</p>

<do type="prev" label="Search">
        <go href="search.wml"/>
</do>

</card>
</wml>
