Flag byte


Flag byte

A flag byte is a generic term for a byte used to indicate conditions within a binary computer. In particular, the byte can be used to show up to 8 discrete conditions. Essentially, each bit represents one flag, capable of showing two states.

Examples

Processor status register

Taking the example of a 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. Zero flag
* Bit 0. Carry 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 byte. 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..A Status register (Flag register)is a collection of flag bits for a processor.

Reading a status byte

To read a status byte, assuming your programming language does not offer this facility by default, is quite easy. You simply need to AND the status byte with a 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 2^5 = 32. 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.

Writing a status byte

Writing a status byte is quite straightforward - to set a bit, 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.

To clear a bit, perform a NOT operation on the mask byte, then AND it with the status byte. The result will have the appropriate flag cleared (set to 0).

To toggle a bit, 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.

Quick reference - mask byte values

*2^0 = 1
*2^1 = 2
*2^2 = 4
*2^3 = 8
*2^4 = 16
*2^5 = 32
*2^6 = 64
*2^7 = 128


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Flag —   [dt. »Flagge«], eine zweiwertige Variable innerhalb eines Registers in einem Prozessor, die zur Kennzeichnung eines bestimmten Zustands verwendet wird. I. d. R. handelt es sich um ein einzelnes Bit. So bezeichnet z. B. das sog. Carry Flag bei… …   Universal-Lexikon

  • Byte — A byte (pronounced bite , IPAEng|baɪt) is the basic unit of measurement of information storage in computer science. In many computer architectures it is a unit of memory addressing, most often consisting of eight bits. A byte is one of the basic… …   Wikipedia

  • Byte — Para la revista estadounidense de informática, véase Byte (revista). Byte es una palabra inglesa (pronunciada [bait] o [ bi.te]), que si bien la Real Academia Española ha aceptado como equivalente a octeto (es decir a ocho bits), para fines… …   Wikipedia Español

  • Byte (журнал) — У этого термина существуют и другие значения, см. Byte. Byte/Россия Специализация: технический Периодичность: ежемесячно Язык: русский Издатель: издательский дом «СК Пресс», издательский дом «Питер» …   Википедия

  • Flag (Datenübertragung) — High Level Data Link Control (HDLC) ist ein von der ISO normiertes Netzprotokoll ISO/IEC 13239:2002. Es ist innerhalb des ISO/OSI Modells in Schicht 2, der Sicherungsschicht einzugliedern. HDLC basiert in seiner Grundstruktur auf dem SDLC… …   Deutsch Wikipedia

  • Ubisoft Blue Byte — Тип дочернее общество Год основания 1988 Прежние названия Blue Byte GmbH Расположение …   Википедия

  • Parity flag — In computer processors the parity flag indicates if the number of set bits is odd or even in the binary representation of the result of the last operation. It is normally a single bit in a processor status register.For example, assume a machine… …   Wikipedia

  • American flag sort — An efficient, in place variant of radix sort that distributes items into hundreds of buckets. The first step counts the number of items in each bucket, and the second step computes where each bucket will start in the array. The last step… …   Wikipedia

  • Extended boot record — An Extended Boot Record (EBR), or Extended Partition Boot Record (EPBR)fn|1, is a descriptor for a logical partition under the common DOS disk drive partitioning system. In that system, when one (and only one) partition record entry in the Master …   Wikipedia

  • Status register — A status register or flag register (also: condition code register, program status word, PSW, etc.) is a collection of flag bits for a processor. An example is the FLAGS register of the x86 architecture. The status register is a hardware register… …   Wikipedia


We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.