Sign in Join
Wiki copytemporary

BitSizeOf: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: BitSizeOf

OldNewLine
1+The compiler intrinsic function **`bitSizeOf`** returns the size of a datum in bits.
2+
3+## definition
4+
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+
7+<div style="text-align: center;">
8+
9+$`\texttt{sizeOf}(\texttt{x}) = \lceil \, \texttt{bitSizeOf}(\texttt{x}) \; / \; 8 \, \rceil`$
10+
11+</div>
12+
13+## application
14+
15+In some cases using `bitSizeOf` can improve your code’s readability:
16+
17+``` pascal
18+program binaryPrint(input, output, stdErr);
19+var
20+ (* in FPC `integer` definition *)
21+ (* depends on the selected mode *)
22+ i: integer;
23+begin
24+ readLn(i);
25+ writeLn(binStr(i, bitSizeOf(i)));
26+end.
27+```
28+
29+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+
31+## see also
32+
33+- [[SizeOf|sizeOf]]
34+
35+---
36+*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).*
37+Category: Pascal

Back to history