xref: /illumos-gate/usr/src/man/man3c/stdc_has_single_bit.3c (revision 3fe455549728ac525df3be56130ad8e075d645d7)
1.\"
2.\" This file and its contents are supplied under the terms of the
3.\" Common Development and Distribution License ("CDDL"), version 1.0.
4.\" You may only use this file in accordance with the terms of version
5.\" 1.0 of the CDDL.
6.\"
7.\" A full copy of the text of the CDDL should have accompanied this
8.\" source.  A copy of the CDDL is also available via the Internet at
9.\" http://www.illumos.org/license/CDDL.
10.\"
11.\"
12.\" Copyright 2024 Oxide Computer Company
13.\"
14.Dd October 27, 2024
15.Dt STDC_HAS_SINGLE_BIT 3C
16.Os
17.Sh NAME
18.Nm stdc_has_single_bit ,
19.Nm stdc_has_single_bit_uc ,
20.Nm stdc_has_single_bit_us ,
21.Nm stdc_has_single_bit_ui ,
22.Nm stdc_has_single_bit_ul ,
23.Nm stdc_has_single_bit_ull
24.Nd determine if only one bit is set
25.Sh LIBRARY
26.Lb libc
27.Sh SYNOPSIS
28.In stdbit.h
29.Ft bool
30.Fo stdc_has_single_bit
31.Fa "generic_value_type value"
32.Fc
33.Ft bool
34.Fo stdc_has_single_bit_uc
35.Fa "unsigned char value"
36.Fc
37.Ft bool
38.Fo stdc_has_single_bit_us
39.Fa "unsigned short value"
40.Fc
41.Ft bool
42.Fo stdc_has_single_bit_ui
43.Fa "unsigned int value"
44.Fc
45.Ft bool
46.Fo stdc_has_single_bit_ul
47.Fa "unsigned long value"
48.Fc
49.Ft bool
50.Fo stdc_has_single_bit_ull
51.Fa "unsigned long long value"
52.Fc
53.Sh DESCRIPTION
54The
55.Fn stdc_has_single_bit
56family of functions determines whether the value has only a single bit
57set.
58.Fa value .
59The function returns
60.Dv true
61if there is exactly one bit whose value is set to one in
62.Fa value .
63.Pp
64The
65.Fn stdc_has_single_bit
66function is generic and will operate on all 8, 16, 32, and 64-bit
67unsigned integers; however, it is only available in C23.
68The other functions all operate on a specific integer type, but
69otherwise behave the same and are available regardless of the C language
70version.
71.Sh RETURN VALUES
72The functions in the
73.Fn stdc_has_single_bit
74family return
75.Dv true
76if exactly one bit is set in
77.Fa value .
78Otherwise,
79.Dv false
80is returned.
81These functions cannot fail.
82.Sh EXAMPLES
83.Sy Example 1
84Printing whether only a single bit is set.
85.Bd -literal
86#include <stdbit.h>
87#include <stdio.h>
88#include <limits.h>
89
90int
91main(void)
92{
93	printf("%s %s %s %s\en",
94	    stdc_has_single_bit_uc(0x23) ? "true" : "false",
95	    stdc_has_single_bit_us(0x0080) ? "true" : "false",
96	    stdc_has_single_bit_ui(0x81941b23) ? "true" : "false",
97	    stdc_has_single_bit_ull(0x00200000000000000) ?
98	    "true" : "false");
99	return (0);
100}
101.Ed
102.Pp
103When compiled and run, this produces:
104.Bd -literal -offset indent
105$ ./a.out
106false true false true
107.Ed
108.Sh INTERFACE STABILITY
109.Sy Committed
110.Sh MT-LEVEL
111.Sy Async-Signal-Safe
112.Sh SEE ALSO
113.Xr stdc_bit_ceil 3C ,
114.Xr stdc_bit_floor 3C ,
115.Xr stdc_bit_width 3C ,
116.Xr stdc_count_ones 3C ,
117.Xr stdc_count_zeros 3C ,
118.Xr stdc_first_leading_one 3C ,
119.Xr stdc_first_leading_zero 3C ,
120.Xr stdc_first_trailing_one 3C ,
121.Xr stdc_first_trailing_zero 3C ,
122.Xr stdc_leading_ones 3C ,
123.Xr stdc_leading_zeros 3C ,
124.Xr stdc_trailing_ones 3C ,
125.Xr stdc_trailing_zeros 3C ,
126.Xr stdbit.h 3HEAD
127