1.\" $NetBSD: bits.3,v 1.16 2014/03/18 18:20:39 riastradh Exp $ 2.\" 3.\" Copyright (c) 2006, 2010 David Young. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or 6.\" without modification, are permitted provided that the following 7.\" conditions are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above 11.\" copyright notice, this list of conditions and the following 12.\" disclaimer in the documentation and/or other materials 13.\" provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY 16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17.\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 18.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID 19.\" YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 21.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26.\" POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd October 17, 2012 29.Dt BITS 3 30.Os 31.Sh NAME 32.Nm __BIT , 33.Nm __BITS , 34.Nm __SHIFTIN , 35.Nm __SHIFTOUT , 36.Nm __SHIFTOUT_MASK 37.Nd "macros for preparing bitmasks and operating on bit fields" 38.Sh SYNOPSIS 39.In sys/param.h 40.In sys/cdefs.h 41.Ft uintmax_t 42.Fn __BIT "n" 43.Ft uintmax_t 44.Fn __BITS "m" "n" 45.Fn __SHIFTIN "v" "mask" 46.Fn __SHIFTOUT "v" "mask" 47.Fn __SHIFTOUT_MASK "mask" 48.Sh DESCRIPTION 49These macros prepare bitmasks, extract bitfields from words, and 50insert bitfields into words. 51A 52.Dq bitfield 53is a span of consecutive bits defined by a bitmask, where 1s select 54the bits in the bitfield. 55.Pp 56Use 57.Fn __BIT 58and 59.Fn __BITS 60to define bitmasks: 61.Pp 62.Bl -tag -width __BITS -offset indent 63.It Fn __BIT "n" 64Return a bitmask with bit 65.Fa n 66set, where the least significant bit is bit 0. 67.It Fn __BITS "m" "n" 68Return a bitmask with bits 69.Fa m 70through 71.Fa n , 72inclusive, set. 73It does not matter whether 74.Fa m No \*[Gt] Fa n 75or 76.Fa m No \*[Lt]= Fa n . 77The least significant bit is bit 0. 78.El 79.Pp 80.Fn __SHIFTIN , 81.Fn __SHIFTOUT , 82and 83.Fn __SHIFTOUT_MASK 84help read and write bitfields from words: 85.Pp 86.Bl -tag -width __SHIFTOUT_MASK -offset indent 87.It Fn __SHIFTIN "v" "mask" 88Left-shift bits 89.Fa v 90into the bitfield defined by 91.Fa mask , 92and return them. 93No side-effects. 94.It Fn __SHIFTOUT "v" "mask" 95Extract and return the bitfield selected by 96.Fa mask 97from 98.Fa v , 99right-shifting the bits so that the rightmost selected bit is at 100bit 0. 101No side-effects. 102.It Fn __SHIFTOUT_MASK "mask" 103Right-shift the bits in 104.Fa mask 105so that the rightmost non-zero bit is at bit 0. 106This is useful for finding the greatest unsigned value that a 107bitfield can hold. 108No side-effects. 109Note that 110.Fn __SHIFTOUT_MASK "m" 111= 112.Fn __SHIFTOUT "m" "m" . 113.El 114.Sh EXAMPLES 115The following example demonstrates basic usage of the 116.Nm bits 117macros: 118.Bd -literal -offset indent 119uint32_t bits, mask, val; 120 121bits = __BITS(2, 3); /* 00001100 */ 122mask = __BIT(2) | __BIT(3); /* 00001100 */ 123 124val = __SHIFTIN(0x03, mask); /* 00001100 */ 125val = __SHIFTOUT(0xf, mask); /* 00000011 */ 126.Ed 127.Sh SEE ALSO 128.Xr bitops 3 , 129.Xr cdefs 3 130.Sh HISTORY 131The 132.Nm bits 133macros first appeared in 134.Xr atw 4 , 135with different names and implementation. 136In their current form these macros appeared in 137.Nx 4.0 . 138.Sh AUTHORS 139The 140.Nm bits 141macros were written by 142.An David Young Aq Mt dyoung@NetBSD.org . 143.An Matt Thomas Aq Mt matt@NetBSD.org 144suggested important improvements to the implementation, and 145contributed the macro names 146.Fn SHIFTIN 147and 148.Fn SHIFTOUT . 149.Sh BUGS 150.Fn __BIT 151and 152.Fn __BITS 153can only express 32-bit bitmasks. 154