GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing en.wikipedia.org — Bit field - Wikipedia A bit field is a data structure that maps to one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected.[1][2] A bit field is most commonly used to represent integral types of known, fixed bit-width, such as single-bit Booleans. The meaning of the individual bits within the field is determined by the programmer; for example, the first bit in a bit field (located at the field's base address) is sometimes used to determine the state of a particular attribute associated with the bit field.[3] Within CPUs and other logic devices, collections of bit fields called flags are commonly used to control or to indicate the outcome of particular operations.[4] Processors have a status register that is composed of flags. For example, if the result of an addition cannot be represented in the destination an arithmetic overflow is set. The flags can be used to decide subsequent operations, such as conditional jump instructions. For example, a A bit field is distinguished from a bit array in that the latter is used to store a large set of bits indexed by integers and is often wider than any integral type supported by the language. Bit fields, on the other hand, typically fit within a machine word,[3] and the denotation of bits is independent of their numerical index.[2] ImplementationBit fields can be used to reduce memory consumption when a program requires a number of integer variables which always will have low values. For example, in many systems, storing an integer value requires two bytes (16-bits) of memory; sometimes the values to be stored actually need only one or two bits. Having a number of these tiny variables share a bit field allows efficient packaging of data in the memory.[5] In C, native implementation-defined bit fields can be created using (possibly For languages lacking native bit fields, or where the programmer wants control over the resulting bit representation, it is possible to manually manipulate bits within a larger word type. In this case, the programmer can set, test, and change the bits in the field using combinations of masking and bitwise operations.[7] ExamplesCBit fields can be used inside structs in C and C++.[6] #include <stdint.h> // opaque and show enum Visibility : uint8_t { NO = 0, YES = 1 }; // line styles enum LineStyle : uint8_t { SOLID = 1, DOTTED = 2, DASHED = 3 }; // Colors enum Color : uint8_t { BLACK = 0, RED = 0b001, GREEN = 0b010, BLUE = 0b100, YELLOW = RED | GREEN, // 011 MAGENTA = RED | BLUE, // 101 CYAN = GREEN | BLUE, // 110 WHITE = RED | GREEN | BLUE // 111 }; const char* colors[8] = {"Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"}; // bit field box properties struct BoxProps { uint32_t opaque : 1; uint32_t fill_color : 3; [[maybe_unused]] uint32_t filler_a : 4; // fill to 8 bits uint32_t show_border : 1; uint32_t border_color : 3; uint32_t border_style : 2; [[maybe_unused]] uint8_t filler_b : 0; // fill to nearest byte (16 bits) uint8_t width : 4; // Split a byte into 2 fields of 4 bits uint8_t height : 4; }; int main() { BoxProps my_box = { .opaque = YES, .fill_color = RED, .show_border = YES, .border_color = BLUE, .border_style = SOLID, .width = 5, .height = 10 }; }
#include <stdint.h> /** * Each of these preprocessor directives defines a single bit, * corresponding to one button on the controller. * Button order matches that of the Nintendo Entertainment System. */ enum Key : uint8_t { KEY_RIGHT = 0b00000001, KEY_LEFT = 0b00000010, KEY_DOWN = 0b00000100, KEY_UP = 0b00001000, KEY_START = 0b00010000, KEY_SELECT = 0b00100000, KEY_B = 0b01000000, KEY_A = 0b10000000 }; // global state uint8_t gameControllerStatus = 0; // Sets the gameControllerStatus using OR void onKeyPressed(uint8_t key) { gameControllerStatus |= key; } // Clears the gameControllerStatus using AND and ~ (binary NOT) void onKeyReleased(uint8_t key) { gameControllerStatus &= ~key; } // Tests whether a bit is set using AND uint8_t isPressed(uint8_t key) { return gameControllerStatus & key; }
RustWhile Rust lacks native C-style bit fields, this is instead achieved using masks. struct BoxProps { raw: [u8; 3] } impl BoxProps { // Byte 0 const OPAQUE: u8 = 0b0000_0001; const FILL_COLOR_MASK: u8 = 0b0000_1110; // Byte 1 const SHOW_BORDER: u8 = 0b0000_0001; const BORDER_COLOR_MASK: u8 = 0b0000_1110; const BORDER_STYLE_MASK: u8 = 0b0011_0000; // Byte 2 const WIDTH_MASK: u8 = 0b0000_1111; const HEIGHT_MASK: u8 = 0b1111_0000; fn opaque(&self) -> bool { self.raw[0] & Self::OPAQUE != 0 } fn set_opaque(&mut self, v: bool) { self.raw[0] = (self.raw[0] & !Self::OPAQUE) | (v as u8); } fn fill_color(&self) -> u8 { (self.raw[0] & Self::FILL_COLOR_MASK) >> 1 } fn set_fill_color(&mut self, v: u8) { self.raw[0] = (self.raw[0] & !Self::FILL_COLOR_MASK) | ((v & 0b111) << 1); } fn width(&self) -> u8 { self.raw[2] & Self::WIDTH_MASK } fn height(&self) -> u8 { (self.raw[2] & Self::HEIGHT_MASK) >> 4 } } Processor status registerThe status register of a processor is a bit field consisting of several flag bits. Each flag bit describes information about the processor's current state.[8] As an example, the status register of the 6502 processor is shown below:
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. For an instance, after an Extracting bits from flag wordsA subset of flags in a flag field may be extracted by ANDing with a mask. A large number of languages support the shift operator (<<) where If the status-byte from a device is 0x67 and the 5th flag bit indicates data-ready. The mask-byte is To check the nth bit from a variable v, perform either of the following: (both are equivalent) bool nth_is_set = (v & (1 << n)) != 0; bool nth_is_set = (v >> n) & 1; Changing bits in flag wordsWriting, 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, 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 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. See also
Notes
References
External links
Hidden categories:
| ||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |