BN_SET_BIT(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸŽ€ BN_SET_BIT(3) β€” bestie's guide to BIGNUM bit ops πŸŽ€

okay so this is a page from the Library Functions Manual and honestly? it's giving "small but mighty" ✨ it lives in libcrypto and it's all about poking individual bits inside a BIGNUM β€” you know, those big scary numbers that don't fit in a normal int. no cap, once you get it you'll be obsessed πŸ’…


🌸 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 ✨

the library is β€œlibcrypto” and you gotta grab the header first, periodt:

#include <openssl/bn.h>

then all the besties in question:

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, const BIGNUM *a);

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

int
BN_rshift1(BIGNUM *r, const BIGNUM *a);

πŸ’– DESCRIPTION πŸ’–

so here's the vibe check on each one πŸ‘‘

  • BN_set_bit() β€” sets bit n in a to 1, literally (a|=(1<<n)). and like, if the number needs to grow to fit that bit? it just expands, no drama, main character energy 🫢
  • BN_clear_bit() β€” sets bit n in a to 0, which is (a&=~(1<<n)). ⚠️ but heads up bestie: an error happens if a is shorter than n bits. it can't clear a bit that isn't even there, and honestly? relatable πŸ’€
  • BN_is_bit_set() β€” just tests whether bit n in a is set. it's the "are we vibing or not" function 🌈
  • BN_mask_bits() β€” truncates a down to an n bit number, aka (a&=~((~0)>>n)). 🚨 warning: an error occurs if a already is shorter than n bits. read that twice, it's backwards from what you'd expect πŸ™ˆ
  • BN_lshift() β€” shifts a left by n bits and drops the result into r, that's (r=a*2^n). note that n must be non-negative. πŸ“Œ
  • BN_lshift1() β€” baby version: shifts a left by one and puts it in r, so (r=2*a) ⭐
  • BN_rshift() β€” shifts a right by n bits, result goes to r, giving (r=a/2^n). again, n must be non-negative πŸ”₯
  • BN_rshift1() β€” right by one, result in r, so (r=a/2) πŸ“
and the iconic part: for the shift functions, r and a may be the same variable. in-place shifting, we love to see it πŸ’œ

🌟 RETURN VALUES 🌟

BN_is_bit_set() returns 1 if the bit is set, and 0 otherwise. simple as that πŸŽ€

all the other functions return 1 for success and 0 on error. and if you're wondering which error it was? you can get the error codes with ERR_get_error(3) β€” go look it up, iykyk πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹


πŸ‘‘ HISTORY πŸ‘‘

BN_set_bit(), BN_clear_bit(), BN_is_bit_set(), BN_mask_bits(), BN_lshift(), BN_lshift1(), BN_rshift(), and BN_rshift1() first appeared in SSLeay 0.5.1 and have been available since OpenBSD 2.4. vintage icons fr fr ⭐


and that's the whole page bestie 🫢 this cutie came to you from Debian, dated June 8, 2025 β€” go forth and shift some bits πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from