BitSizeOf: what changed
Revision 2 to 3. 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 | - | ||
| 5 | + | `BitSizeOf` produces different results only for members of [[Bitpacked|bitpacked]] 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: | |
| 6 | 6 | ||
| 7 | - | ||
| 7 | + | $`\texttt{sizeOf}(\texttt{x}) = \lceil \, \texttt{bitSizeOf}(\texttt{x}) \; / \; 8 \, \rceil`$ | |
| 8 | 8 | ||
| 9 | 9 | ## application | |
| 10 | 10 | ||
| 11 | - | ||
| 12 | - | ||
| 11 | + | In some cases using `bitSizeOf` can improve your code’s readability: | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 13 | 14 | program binaryPrint(input, output, stdErr); | |
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 15 | + | var | |
| 16 | + | (* in FPC `integer` definition *) | |
| 17 | + | (* depends on the selected mode *) | |
| 18 | + | i: integer; | |
| 19 | + | begin | |
| 20 | + | readLn(i); | |
| 21 | + | writeLn(binStr(i, bitSizeOf(i))); | |
| 22 | + | end. | |
| 22 | 23 | ``` | |
| 23 | 24 | ||
| 24 | - | ||
| 25 | + | 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. | |
| 25 | 26 | ||
| 26 | 27 | ## see also | |
| 27 | 28 | ||
| 28 | - | ||
| 29 | + | - [[SizeOf|sizeOf]] | |
| 29 | 30 | ||
| 30 | 31 | --- | |
| 31 | - | ||
| 32 | + | *From the Free Pascal wiki, page "BitSizeOf", as it stood on 2021-07-03 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. 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).* | |
| 33 | + | Category: Pascal |
