en:pfw:bit-array
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:pfw:bit-array [2023-09-04 18:11] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | en:pfw:bit-array [2023-09-04 18:11] (current) – ↷ Seite von pfw:bit-array nach en:pfw:bit-array verschoben uho | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{pfw: | ||
| + | ====== Bit array data structure ====== | ||
| + | |||
| + | ===== Bit array idea ===== | ||
| + | |||
| + | To store many on/off events or a present/not present note, we could use an array of bit flags to save space. | ||
| + | |||
| + | ===== Implementation ===== | ||
| + | |||
| + | The implementation assumes a byte addressed machine. The number of bits is rounded up to the number of cells needed to store the flags. The cell size in bytes is used and multiplied by 8 to calculate the number of bits in a cell. | ||
| + | |||
| + | {{https:// | ||
| + | |||
| + | This i a one cell array on a machine with 16-bit cell size. Three of the 16-bits are high. Note that only 4 cells (8 bytes) are required to keep track of 64 flags. | ||
| + | |||
| + | A note on '' | ||
| + | |||
| + | ===== Pseudo code for a bit array ===== | ||
| + | |||
| + | < | ||
| + | Function: BITARRAY | ||
| + | Define: ( u -- ) | ||
| + | Reserve RAM space rounded to the next higher cell | ||
| + | to allocate space for a bit-array with at least u-bits. | ||
| + | Action: ( -- a ) | ||
| + | Leave start address of this bit-array | ||
| + | |||
| + | Function: *SET ( bit-nr addr -- ) | ||
| + | Calculate from bit number and bit-array address the | ||
| + | correct bit-mask and cell-address. OR the bit-mask with | ||
| + | the contents of the cell-address and store it back | ||
| + | |||
| + | Function: *CLR ( bit-nr addr -- ) | ||
| + | Calculate from bit number and bit-array address the | ||
| + | correct bit-mask and cell-address. AND the inverted bit-mask | ||
| + | with the contents of the cell-address and store it back | ||
| + | |||
| + | Function: GET* ( bit-nr addr – mask|0 ) | ||
| + | Calculate from bit number and array address the | ||
| + | correct bit mask and cell-address. AND the bitmask with | ||
| + | the contents of the cell-address, | ||
| + | |||
| + | Function: ZERO ( addr -- ) | ||
| + | Clear the bit array starting at address completely | ||
| + | </ | ||
| + | |||
| + | ===== Generic Forth bit array example: ===== | ||
| + | |||
| + | <code forth> | ||
| + | Extra words: @+ | ||
| + | : ?ABORT ( fl -- ) 0= 0= throw ; | ||
| + | |||
| + | Words with hardware dependencies: | ||
| + | : **BIS ( mask addr -- ) tuck @ or swap ! ; | ||
| + | : **BIC ( mask addr -- ) > | ||
| + | : BIT** ( mask addr -- 0|b ) @ and ; | ||
| + | |||
| + | 8 cells constant #BITS \ Bits in a cell | ||
| + | |||
| + | : > | ||
| + | : > | ||
| + | |||
| + | : LOC ( nr a1 -- bit a2 ) \ Bit location in cell address | ||
| + | 2dup >len < 0= ? | ||
| + | @+ 1- >r @ \ nr adr | ||
| + | >r dup #bits 1- and \ nr bit-nr | ||
| + | 1 swap lshift | ||
| + | swap #bits / \ nr / bits in a cell | ||
| + | r> swap r> and cells + \ Mask excess bits, leave bit-array offset | ||
| + | ; \ Leave bit & word-adr | ||
| + | |||
| + | : BITARRAY | ||
| + | create | ||
| + | #bits /mod swap if 1+ then dup , \ Round to the next cell | ||
| + | here , cells allot ; \ Save RAM pointer, reserve RAM in ROM Forth | ||
| + | \ here cell+ , cells allot ; \ Save pointer, reserve RAM in RAM Forth | ||
| + | |||
| + | : *SET ( nr a -- ) loc **bis ; \ Set bit nr in array a | ||
| + | : *CLR ( nr a -- ) loc **bic ; \ Erase bit nr from array a | ||
| + | : GET* ( nr a -- 0|msk ) loc bit** ; \ Bit nr set in array a? | ||
| + | : ZERO ( a -- ) dup > | ||
| + | </ | ||
| + | |||
| + | ===== Implementations ===== | ||
| + | |||
| + | Have a look at the sub directories for implementations for different systems. | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | ===== Contributions ===== | ||
| + | |||
| + | < | ||