BN_SET_BIT(3ossl)

πŸ’… BN_SET_BIT β€” bit operations on BIGNUMs (OpenSSL manual, 3ossl)

okay bestie, sit down, hydrate, because we're about to do bit surgery on big numbers and honestly? it's giving iconic. this is the OpenSSL page for poking individual bits inside a BIGNUM β€” setting them, clearing them, checking them, chopping them off, and sliding the whole thing left or right. no cap, it's the whole glow-up kit. πŸŽ€


✨ NAME ✨

BN_set_bit, BN_clear_bit, BN_is_bit_set, BN_mask_bits, BN_lshift, BN_lshift1, BN_rshift, BN_rshift1 β€” bit operations on BIGNUMs πŸ¦‹


✨ SYNOPSIS ✨

#include <openssl/bn.h>

int BN_set_bit(BIGNUM *a, int n);
int BN_clear_bit(BIGNUM *a, int n);

int BN_is_bit_set(const BIGNUM *a, int n);

int BN_mask_bits(BIGNUM *a, int n);

int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
int BN_lshift1(BIGNUM *r, BIGNUM *a);

int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
int BN_rshift1(BIGNUM *r, BIGNUM *a);

✨ DESCRIPTION ✨

BN_set_bit() flips bit n inside a up to 1 β€” the math vibe is a|=(1<<n). and like, if the number isn't big enough to even have that bit? it just grows. expansion era. πŸ’…

BN_clear_bit() flips bit n inside a down to 0 β€” that's a&=~(1<<n). but heads up bestie: if a is shorter than n bits, you get an error. no bit, no service. 😭

BN_is_bit_set() just vibe-checks whether bit n in a is set. that's it. that's the function. πŸ‘‘

BN_mask_bits() truncates a down to an n bit number β€” a&=~((~0)<<n). two ways to catch an error here:

  • if n is negative 🚫
  • if the internal representation of a is already shorter than n bits

and okay, about that second one β€” the internal representation depends on the platform's word size, so you can safely ignore that error. fr fr. if you actually need to know the exact number of bits, go ask BN_num_bits(3). 🧚

BN_lshift() shifts a left by n bits and drops the result into r β€” r=a*2^n. note that n must be nonnegative. and BN_lshift1() is the lil one-bit version: shifts a left by one and puts it in r β€” r=2*a. πŸ“

BN_rshift() shifts a right by n bits and places the result in r β€” r=a/2^n. again, n must be nonnegative. and BN_rshift1() shifts a right by one into r β€” r=a/2. 🌸

and the bestie bonus for all the shift girlies: for the shift functions, r and a may be the same variable. in-place slay. πŸ’–


✨ RETURN VALUES ✨

BN_is_bit_set() returns 1 if the bit is set, and 0 otherwise. simple. binary. main character energy.

every other function returns 1 for success and 0 on error. wanna know what went wrong? grab the error codes with ERR_get_error(3). 🫢


✨ SEE ALSO ✨

BN_num_bytes(3), BN_add(3)


Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the whole page, bestie πŸŽ€ this was OpenSSL 3.5.9, dated 2026-09-29, and it ate, no crumbs. go set some bits. πŸ’œβœ¨πŸ¦‹

πŸ“¦ where this page comes from