BitSizeOf: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | The compiler intrinsic function **`bitSizeOf`** returns the size of a datum in bits. | |
| 2 | 2 | ||
| 3 | 3 | ## definition | |
| 4 | 4 | ||
| 5 | - | ||
| 6 | - | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 5 | + | `BitSizeOf` produces different results only for members of [`bitpacked`](</index.php?title=Bitpacked&action=edit&redlink=1> "Bitpacked \(page does not exist\)") structures. In all other cases the result of `bitSizeOf` is simply `8 * sizeOf(x)` (or `sizeOf(x) shl 3`). To put this relation in other words: | |
| 10 | 6 | ||
| 11 | - | ||
| 7 | + | [math]\displaystyle{ \texttt{sizeOf}(\texttt{x}) = \lceil \, \texttt{bitSizeOf}(\texttt{x}) \; / \; 8 \, \rceil }[/math] | |
| 12 | 8 | ||
| 13 | 9 | ## application | |
| 14 | 10 | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 11 | + | In some cases using `bitSizeOf` can improve your code’s readability: | |
| 12 | + | ``` | |
| 18 | 13 | program binaryPrint(input, output, stdErr); | |
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 14 | + | var | |
| 15 | + | (* in FPC `integer` definition *) | |
| 16 | + | (* depends on the selected mode *) | |
| 17 | + | i: integer; | |
| 18 | + | begin | |
| 19 | + | readLn(i); | |
| 20 | + | writeLn(binStr(i, bitSizeOf(i))); | |
| 21 | + | end. | |
| 27 | 22 | ``` | |
| 28 | 23 | ||
| 29 | - | ||
| 24 | + | If your code makes presumptions about memory requirements, e. g. by using an algorithm exploiting certain properties of your `bitpacked` data type, you may want to safeguard your code with some [[Conditional compilation|conditional compilation]], may be even just for documentation purposes. | |
| 30 | 25 | ||
| 31 | 26 | ## see also | |
| 32 | 27 | ||
| 33 | - | ||
| 28 | + | * [[SizeOf|`sizeOf`]] | |
| 34 | 29 | ||
| 35 | 30 | --- | |
| 36 | - | ||
| 37 | - | ||
| 31 | + | *From the Free Pascal wiki, page "BitSizeOf", as the Internet Archive kept it on 2024-03-04. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
