1.\" $OpenBSD: BN_set_bit.3,v 1.8 2021/11/30 18:34:35 tb Exp $ 2.\" OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100 3.\" 4.\" This file was written by Ulf Moeller <ulf@openssl.org>. 5.\" Copyright (c) 2000, 2015 The OpenSSL Project. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in 16.\" the documentation and/or other materials provided with the 17.\" distribution. 18.\" 19.\" 3. All advertising materials mentioning features or use of this 20.\" software must display the following acknowledgment: 21.\" "This product includes software developed by the OpenSSL Project 22.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 23.\" 24.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25.\" endorse or promote products derived from this software without 26.\" prior written permission. For written permission, please contact 27.\" openssl-core@openssl.org. 28.\" 29.\" 5. Products derived from this software may not be called "OpenSSL" 30.\" nor may "OpenSSL" appear in their names without prior written 31.\" permission of the OpenSSL Project. 32.\" 33.\" 6. Redistributions of any form whatsoever must retain the following 34.\" acknowledgment: 35.\" "This product includes software developed by the OpenSSL Project 36.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 37.\" 38.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49.\" OF THE POSSIBILITY OF SUCH DAMAGE. 50.\" 51.Dd $Mdocdate: November 30 2021 $ 52.Dt BN_SET_BIT 3 53.Os 54.Sh NAME 55.Nm BN_set_bit , 56.Nm BN_clear_bit , 57.Nm BN_is_bit_set , 58.Nm BN_mask_bits , 59.Nm BN_lshift , 60.Nm BN_lshift1 , 61.Nm BN_rshift , 62.Nm BN_rshift1 63.Nd bit operations on BIGNUMs 64.Sh SYNOPSIS 65.In openssl/bn.h 66.Ft int 67.Fo BN_set_bit 68.Fa "BIGNUM *a" 69.Fa "int n" 70.Fc 71.Ft int 72.Fo BN_clear_bit 73.Fa "BIGNUM *a" 74.Fa "int n" 75.Fc 76.Ft int 77.Fo BN_is_bit_set 78.Fa "const BIGNUM *a" 79.Fa "int n" 80.Fc 81.Ft int 82.Fo BN_mask_bits 83.Fa "BIGNUM *a" 84.Fa "int n" 85.Fc 86.Ft int 87.Fo BN_lshift 88.Fa "BIGNUM *r" 89.Fa "const BIGNUM *a" 90.Fa "int n" 91.Fc 92.Ft int 93.Fo BN_lshift1 94.Fa "BIGNUM *r" 95.Fa "const BIGNUM *a" 96.Fc 97.Ft int 98.Fo BN_rshift 99.Fa "BIGNUM *r" 100.Fa "const BIGNUM *a" 101.Fa "int n" 102.Fc 103.Ft int 104.Fo BN_rshift1 105.Fa "BIGNUM *r" 106.Fa "const BIGNUM *a" 107.Fc 108.Sh DESCRIPTION 109.Fn BN_set_bit 110sets bit 111.Fa n 112in 113.Fa a 114to 1 115.Pq Li a|=(1<<n) . 116The number is expanded if necessary. 117.Pp 118.Fn BN_clear_bit 119sets bit 120.Fa n 121in 122.Fa a 123to 0 124.Pq Li a&=~(1<<n) . 125An error occurs if 126.Fa a 127is shorter than 128.Fa n 129bits. 130.Pp 131.Fn BN_is_bit_set 132tests if bit 133.Fa n 134in 135.Fa a 136is set. 137.Pp 138.Fn BN_mask_bits 139truncates 140.Fa a 141to an 142.Fa n 143bit number 144.Pq Li a&=~((~0)>>n) . 145An error occurs if 146.Fa a 147already is shorter than 148.Fa n 149bits. 150.Pp 151.Fn BN_lshift 152shifts 153.Fa a 154left by 155.Fa n 156bits and places the result in 157.Fa r 158.Pq Li r=a*2^n . 159Note that 160.Fa n 161must be non-negative. 162.Fn BN_lshift1 163shifts 164.Fa a 165left by one and places the result in 166.Fa r 167.Pq Li r=2*a . 168.Pp 169.Fn BN_rshift 170shifts 171.Fa a 172right by 173.Fa n 174bits and places the result in 175.Fa r 176.Pq Li r=a/2^n . 177Note that 178.Fa n 179must be non-negative. 180.Fn BN_rshift1 181shifts 182.Fa a 183right by one and places the result in 184.Fa r 185.Pq Li r=a/2 . 186.Pp 187For the shift functions, 188.Fa r 189and 190.Fa a 191may be the same variable. 192.Sh RETURN VALUES 193.Fn BN_is_bit_set 194returns 1 if the bit is set, 0 otherwise. 195.Pp 196All other functions return 1 for success, 0 on error. 197The error codes can be obtained by 198.Xr ERR_get_error 3 . 199.Sh SEE ALSO 200.Xr BN_add 3 , 201.Xr BN_new 3 , 202.Xr BN_num_bytes 3 , 203.Xr BN_set_negative 3 , 204.Xr BN_zero 3 205.Sh HISTORY 206.Fn BN_set_bit , 207.Fn BN_clear_bit , 208.Fn BN_is_bit_set , 209.Fn BN_mask_bits , 210.Fn BN_lshift , 211.Fn BN_lshift1 , 212.Fn BN_rshift , 213and 214.Fn BN_rshift1 215first appeared in SSLeay 0.5.1 and have been available since 216.Ox 2.4 . 217