BN_SET_BIT(3SSL)

πŸ’… BN_SET_BIT(3SSL) β€” bit ops on BIGNUMs, bestie edition

okay so you've found yourself in the OpenSSL manual, section 3SSL, staring at a page all about poking individual bits inside a BIGNUM. genuinely iconic behavior. let's get into it ✨


πŸŽ€ 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);

look at all those little guys. each one is a whole personality, fr fr πŸ’–


🌸 DESCRIPTION

BN_set_bit() β€” flips bit n in a to 1, which is literally just a|=(1<<n) if you wanna think about it that way. and if the number needs to grow to hold that bit? it just grows. no drama, she expands 🫢

BN_clear_bit() β€” flips bit n in a to 0, aka a&=~(1<<n). ⚠️ but heads up bestie: if a is shorter than n bits, that's an error. she can't clear a bit that isn't there πŸ’€

BN_is_bit_set() β€” the vibe check πŸ‘‘ asks the question: is bit n in a actually set? that's it, that's the whole job.

BN_mask_bits() β€” truncates a down to an n bit number, mathematically a&=~((~0)<<n). βœ‚οΈ two ways to fail here: an error happens if n is negative, and an error is also returned if the internal representation of a is already shorter than n bits. and now the tea β˜• β€” that internal representation depends on the platform's word size, so that particular error can be safely ignored. if you actually need the exact number of bits, go use BN_num_bits(3). iykyk 🌈

BN_lshift() β€” shifts a left by n bits and drops the result into r, i.e. r=a*2^n. note that n must be nonnegative 🚨

BN_lshift1() β€” shifts a left by exactly one and puts the result in r, i.e. r=2*a. one bit. tiny move. ate πŸ’…

BN_rshift() β€” shifts a right by n bits and places the result in r, i.e. r=a/2^n. and again, note that n must be nonnegative 🚨

BN_rshift1() β€” shifts a right by one and places the result in r, i.e. r=a/2. πŸ“

✨ cute lil bonus fact for the shift functions: r and a may be the same variable. in-place era, no cap πŸ™ˆ

πŸ’œ RETURN VALUES

  • BN_is_bit_set() returns 1 if the bit is set, 0 otherwise.
  • everything else returns 1 for success and 0 on error. πŸ’€
  • need the actual error codes? grab them with ERR_get_error(3). periodt.

🌈 SEE ALSO

BN_num_bytes(3), BN_add(3)

go read those too, they're giving main character energy 🧚


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 BN_SET_BIT(3SSL) from OpenSSL 4.0.3 and the page is dated 2026-09-29 β€” so go set some bits, clear some bits, shift things around, and stay slay πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from