|
|
| Line 1: |
Line 1: |
| {{Unreferenced|date=December 2009}}
| | Greetings. The writer's name is Phebe and she feels comfortable when individuals use the complete name. One of the std home test issues he enjoys most is ice skating but he is having difficulties to [http://www.ncbi.Nlm.nih.gov/pubmed/176017 discover time] for it. North Dakota is her birth place but she will have to transfer 1 day or another. at home std test Since she was eighteen she's been working as a receptionist but her promotion by no means comes.<br><br>My site; [http://cherryjuice.net/xe/?document_srl=1446131 http://cherryjuice.net/xe/?document_srl=1446131] |
| {{merge|Bit field|date=April 2013}}
| |
| | |
| In [[computer programming]], a '''flag word''' can refer to one or more [[bit]]s that are used to store a [[binary numeral system|binary]] value or [[code]] that has an assigned meaning, but can refer to uses of other data types. Flags are typically found as members of a defined [[data structure]], such as a [[Row (database)|database record]], and the meaning of the value contained in a flag will generally be defined in relation to the data structure it is part of. In many cases, the binary value of a flag will be understood to represent one of several possible states or statuses. In other cases, the binary values may represent one or more attributes in a [[bit field]], often related to abilities or permissions, such as "can be written to" or "can be deleted". However, there are many other possible meanings that can be assigned to flag values. One common use of flags is to mark or designate data structures for future processing.
| |
| | |
| Within [[microprocessor]]s and other logic devices, flags are commonly used to control or indicate the intermediate or final state or outcome of different operations. Microprocessors typically have, for example, a [[status register]] that is composed of such flags, and the flags are used to indicate various post-operation conditions, such as when there has been an [[arithmetic overflow]]. The flags can be utilized in subsequent operations, such as in processing conditional [[Branch (computer science)|jump instruction]]s. For example a ''je'' (Jump if Equal) instruction in the [[X86 assembly language#Programming flow|X86 assembly language]] will result in a jump if the Z (zero) flag was set by some previous operation.
| |
| | |
| A [[Command-line interface#Command-line option|command line switch]] is also known as a flag. [[Command-line interface|Command line]] programs often start with an option [[Parsing|parser]] that translates command line switches into flags in the sense of this article.
| |
| | |
| ==Examples==
| |
| | |
| ===Processor status register===
| |
| Taking the example of a [[MOS Technology 6502|6502]] processor's [[status register]], the following information was held within 8 bits:
| |
| | |
| * Bit 7. Negative flag
| |
| * Bit 6. Overflow flag
| |
| * Bit 5. Unused
| |
| * Bit 4. Break flag
| |
| * Bit 3. Decimal flag
| |
| * Bit 2. Interrupt-disable flag
| |
| * Bit 1. Carry flag
| |
| * Bit 0. Zero flag
| |
| | |
| As you can see, a lot of information about the state of the processor can be packed into 8 bits.
| |
| | |
| ===Unix process exit code===
| |
| A more general example would be the use of a [[Unix]] [[exit status]] as a flag word. In this case, the exit code is by the programmer to pass status information to another process. An imaginary program which returns the status of 8 burglar alarm switches connected to the printer port could set each of the bits in the exit code in turn, depending on whether the switches are closed or open.
| |
| | |
| ==Extracting bits from flag words==
| |
| | |
| To read a status byte, assuming your programming language does not offer this facility by default, is quite easy. You simply need to [[Logical conjunction|AND]] the status byte with a [[Mask (computing)|mask byte]]. The mask byte should have only the bit corresponding to the flag you want to read set, as in the example below.
| |
| | |
| Suppose that status byte 103 (decimal) is returned, and that we want to check flag bit 5.
| |
| | |
| The flag we want to read is number 5 (counting from zero) - so the mask byte will be <math>2^5 = 32</math>. ANDing 32 with 103 gives 32, which means the flag bit is set. If the flag bit was not set, the result would have been 0.
| |
| | |
| In modern computing, the [[logical shift|shift]] operator (<<) can be used to quickly perform the power-of-two. In general, a mask with the Nth bit set can be computed as
| |
| (1 << n)
| |
| | |
| Thus to check the Nth bit from a variable '''v''', we can perform the operation
| |
| bool nth_is_set = ('''v''' & (1 << n)) != 0
| |
| | |
| ==Changing bits in flag words==
| |
| | |
| 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, [[Logical operator|OR]] the status byte with a mask byte. Any bits set in the mask byte or the status byte will be set in the result.
| |
| <source lang="c">
| |
| int setBit(int val, int bit_position)
| |
| {
| |
| return val | (1 << bit_position);
| |
| }
| |
| </source>
| |
| To clear a bit, perform a [[Negation|NOT]] operation on the mask byte, then AND it with the status byte. The result will have the appropriate flag cleared (set to 0).
| |
| <source lang="c">
| |
| int clearBit(int val, int bit_position)
| |
| {
| |
| return val & ~(1 << bit_position);
| |
| }
| |
| </source>
| |
| To toggle a bit, [[Exclusive disjunction|XOR]] the status byte and the mask byte. This will set a bit if it is cleared or clear a bit if it is set.
| |
| <source lang="c">
| |
| int toggleBit(int val, int bit_position)
| |
| {
| |
| return val ^ (1 << bit_position);
| |
| }
| |
| </source>
| |
| | |
| ==See also==
| |
| *[[Bit field]]
| |
| *[[Bit array]]
| |
| *[[Status register]]
| |
| *[[FLAGS register (computing)]]
| |
| *[[Program status word]]
| |
| *[[Control register]]
| |
| | |
| [[Category:Programming idioms]]
| |
| [[Category:Operating system technology]]
| |
| [[Category:Central processing unit]]
| |
| [[Category:Digital registers]]
| |
Greetings. The writer's name is Phebe and she feels comfortable when individuals use the complete name. One of the std home test issues he enjoys most is ice skating but he is having difficulties to discover time for it. North Dakota is her birth place but she will have to transfer 1 day or another. at home std test Since she was eighteen she's been working as a receptionist but her promotion by no means comes.
My site; http://cherryjuice.net/xe/?document_srl=1446131