BN_SET_BIT(3)
π 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 to1, 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 to0, 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
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2000, 2015 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms