Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mask (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Arguments to functions === In programming languages such as [[C (language)|C]], bit fields are a useful way to pass a set of named Boolean arguments to a function. For example, in the graphics API [[OpenGL]], there is a command, <code>glClear()</code> which clears the screen or other buffers. It can clear up to four buffers (the color, depth, accumulation, and [[stencil buffer]]s), so the API authors could have had it take four arguments. But then a call to it would look like <syntaxhighlight lang="c"> glClear(1,1,0,0); // This is not how glClear actually works and would make for unstable code.</syntaxhighlight> which is not very descriptive. Instead there are four defined field bits, <code>GL_COLOR_BUFFER_BIT</code>, <code>GL_DEPTH_BUFFER_BIT</code>, <code>GL_ACCUM_BUFFER_BIT</code>, and <code>GL_STENCIL_BUFFER_BIT</code> and <code>glClear()</code> is declared as <syntaxhighlight lang="c"> void glClear(GLbitfield bits);</syntaxhighlight> Then a call to the function looks like this <syntaxhighlight lang="c"> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);</syntaxhighlight> Internally, a function taking a bitfield like this can use binary <code>and</code> to extract the individual bits. For example, an implementation of <code>glClear()</code> might look like: <syntaxhighlight lang="c"> void glClear(GLbitfield bits) { if ((bits & GL_COLOR_BUFFER_BIT) != 0) { // Clear color buffer. } if ((bits & GL_DEPTH_BUFFER_BIT) != 0) { // Clear depth buffer. } if ((bits & GL_ACCUM_BUFFER_BIT) != 0) { // Clear accumulation buffer. } if ((bits & GL_STENCIL_BUFFER_BIT) != 0) { // Clear stencil buffer. } }</syntaxhighlight> The advantage to this approach is that function argument overhead is decreased. Since the minimum datum size is one byte, separating the options into separate arguments would be wasting seven bits per argument and would occupy more stack space. Instead, functions typically accept one or more 32-bit integers, with up to 32 option bits in each. While elegant, in the simplest implementation this solution is not [[type safety|type-safe]]. A <code>GLbitfield</code> is simply defined to be an <code>unsigned int</code>, so the compiler would allow a meaningless call to <code>glClear(42)</code> or even <code>glClear(GL_POINTS)</code>. In [[C++]] an alternative would be to create a class to encapsulate the set of arguments that glClear could accept and could be cleanly encapsulated in a library.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)