Complete set of bitwise operations including logical operations (AND, OR, XOR), bit shifts, and individual bit manipulation for big numbers.
Standard bitwise logical operations with support for both signed and unsigned variants.
/**
* Bitwise OR operation
* @param {BN} num - Number to OR with
* @returns {BN} New BN instance with the result
*/
or(num: BN): BN;
/**
* Bitwise OR operation in-place
* @param {BN} num - Number to OR with
* @returns {BN} This BN instance modified with the result
*/
ior(num: BN): BN;
/**
* Unsigned bitwise OR operation
* @param {BN} num - Number to OR with
* @returns {BN} New BN instance with the result (always positive)
*/
uor(num: BN): BN;
/**
* Unsigned bitwise OR operation in-place
* @param {BN} num - Number to OR with
* @returns {BN} This BN instance modified with the result
*/
iuor(num: BN): BN;/**
* Bitwise AND operation
* @param {BN} num - Number to AND with
* @returns {BN} New BN instance with the result
*/
and(num: BN): BN;
/**
* Bitwise AND operation in-place
* @param {BN} num - Number to AND with
* @returns {BN} This BN instance modified with the result
*/
iand(num: BN): BN;
/**
* Unsigned bitwise AND operation
* @param {BN} num - Number to AND with
* @returns {BN} New BN instance with the result (always positive)
*/
uand(num: BN): BN;
/**
* Unsigned bitwise AND operation in-place
* @param {BN} num - Number to AND with
* @returns {BN} This BN instance modified with the result
*/
iuand(num: BN): BN;/**
* Bitwise XOR operation
* @param {BN} num - Number to XOR with
* @returns {BN} New BN instance with the result
*/
xor(num: BN): BN;
/**
* Bitwise XOR operation in-place
* @param {BN} num - Number to XOR with
* @returns {BN} This BN instance modified with the result
*/
ixor(num: BN): BN;
/**
* Unsigned bitwise XOR operation
* @param {BN} num - Number to XOR with
* @returns {BN} New BN instance with the result (always positive)
*/
uxor(num: BN): BN;
/**
* Unsigned bitwise XOR operation in-place
* @param {BN} num - Number to XOR with
* @returns {BN} This BN instance modified with the result
*/
iuxor(num: BN): BN;Usage Examples:
const BN = require('bn.js');
const a = new BN('0b11110000', 2); // 240
const b = new BN('0b10101010', 2); // 170
// OR operations
const orResult = a.or(b);
console.log(orResult.toString(2)); // "11111010" (250)
// AND operations
const andResult = a.and(b);
console.log(andResult.toString(2)); // "10100000" (160)
// XOR operations
const xorResult = a.xor(b);
console.log(xorResult.toString(2)); // "1011010" (90)
// In-place operations (on clones)
const a2 = a.clone();
a2.ior(b);
console.log(a2.toString(2)); // "11111010" (same as orResult)Bitwise NOT operation with specified bit width.
/**
* Bitwise NOT operation with specified width
* @param {number} width - Bit width for the NOT operation
* @returns {BN} New BN instance with bits flipped within the specified width
*/
notn(width: number): BN;
/**
* Bitwise NOT operation with specified width in-place
* @param {number} width - Bit width for the NOT operation
* @returns {BN} This BN instance with bits flipped within the specified width
*/
inotn(width: number): BN;Usage Examples:
const BN = require('bn.js');
const num = new BN('0b11110000', 2); // 240
// NOT operation with 8-bit width
const notResult = num.notn(8);
console.log(notResult.toString(2)); // "1111" (15) - lower 4 bits flipped
// NOT operation with 16-bit width
const notResult16 = num.notn(16);
console.log(notResult16.toString(2)); // "1111000000001111" (61455)Left and right bit shift operations with both signed and unsigned variants.
/**
* Shift bits left
* @param {number} bits - Number of bits to shift
* @returns {BN} New BN instance shifted left
*/
shln(bits: number): BN;
/**
* Shift bits left in-place
* @param {number} bits - Number of bits to shift
* @returns {BN} This BN instance shifted left
*/
ishln(bits: number): BN;
/**
* Unsigned shift bits left
* @param {number} bits - Number of bits to shift
* @returns {BN} New BN instance shifted left (unsigned)
*/
ushln(bits: number): BN;
/**
* Unsigned shift bits left in-place
* @param {number} bits - Number of bits to shift
* @returns {BN} This BN instance shifted left (unsigned)
*/
iushln(bits: number): BN;/**
* Shift bits right
* @param {number} bits - Number of bits to shift
* @returns {BN} New BN instance shifted right
*/
shrn(bits: number): BN;
/**
* Shift bits right in-place
* @param {number} bits - Number of bits to shift
* @param {number} [hint] - Hint for optimization
* @param {BN} [extended] - BN to store extended bits
* @returns {BN} This BN instance shifted right
*/
ishrn(bits: number, hint?: number, extended?: BN): BN;
/**
* Unsigned shift bits right
* @param {number} bits - Number of bits to shift
* @returns {BN} New BN instance shifted right (unsigned)
*/
ushrn(bits: number): BN;
/**
* Unsigned shift bits right in-place
* @param {number} bits - Number of bits to shift
* @param {number} [hint] - Hint for optimization
* @param {BN} [extended] - BN to store extended bits
* @returns {BN} This BN instance shifted right (unsigned)
*/
iushrn(bits: number, hint?: number, extended?: BN): BN;Usage Examples:
const BN = require('bn.js');
const num = new BN('240'); // 11110000 in binary
// Left shifts (multiply by powers of 2)
console.log(num.shln(1).toString()); // "480" (240 * 2)
console.log(num.shln(2).toString()); // "960" (240 * 4)
console.log(num.shln(3).toString()); // "1920" (240 * 8)
// Right shifts (divide by powers of 2)
console.log(num.shrn(1).toString()); // "120" (240 / 2)
console.log(num.shrn(2).toString()); // "60" (240 / 4)
console.log(num.shrn(4).toString()); // "15" (240 / 16)
// Working with binary representation
const binary = new BN('0b11110000', 2);
console.log(binary.shln(2).toString(2)); // "1111000000" (shifted left 2)
console.log(binary.shrn(2).toString(2)); // "111100" (shifted right 2)Operations for testing, setting, and manipulating individual bits.
/**
* Test if a specific bit is set
* @param {number} bit - Bit position to test (0-based from right)
* @returns {boolean} True if the bit is set (1), false if clear (0)
*/
testn(bit: number): boolean;
/**
* Set or clear a specific bit
* @param {number} bit - Bit position to modify (0-based from right)
* @param {boolean} val - Value to set (true for 1, false for 0)
* @returns {BN} This BN instance with the bit modified
*/
setn(bit: number, val: boolean): BN;
/**
* Increment the bit at specified position (add 2^bit)
* @param {number} bit - Bit position to increment
* @returns {BN} This BN instance incremented
*/
bincn(bit: number): BN;Usage Examples:
const BN = require('bn.js');
const num = new BN('240'); // 11110000 in binary
// Test individual bits
console.log(num.testn(0)); // false (bit 0 is 0)
console.log(num.testn(4)); // true (bit 4 is 1)
console.log(num.testn(7)); // true (bit 7 is 1)
// Set individual bits
const modified = num.clone();
modified.setn(0, true); // Set bit 0 to 1
console.log(modified.toString()); // "241" (11110001)
modified.setn(7, false); // Set bit 7 to 0
console.log(modified.toString()); // "113" (01110001)
// Increment specific bit positions
const incremented = new BN('0');
incremented.bincn(3); // Add 2^3 = 8
console.log(incremented.toString()); // "8"
incremented.bincn(0); // Add 2^0 = 1
console.log(incremented.toString()); // "9"Operations for applying bit masks and working with specific bit ranges.
/**
* Keep only the lower bits, clearing higher bits
* @param {number} bits - Number of lower bits to keep
* @returns {BN} New BN instance with only lower bits
*/
maskn(bits: number): BN;
/**
* Keep only the lower bits in-place, clearing higher bits
* @param {number} bits - Number of lower bits to keep
* @returns {BN} This BN instance with only lower bits
*/
imaskn(bits: number): BN;
/**
* AND with a JavaScript number (for low bits)
* @param {number} num - JavaScript number to AND with
* @returns {number} Result of AND operation as JavaScript number
*/
andln(num: number): number;Usage Examples:
const BN = require('bn.js');
const num = new BN('0xFF00FF00', 16); // 4278255360
// Mask to keep only lower 8 bits
const masked8 = num.maskn(8);
console.log(masked8.toString(16)); // "0" (lower 8 bits are all 0)
// Mask to keep only lower 16 bits
const masked16 = num.maskn(16);
console.log(masked16.toString(16)); // "ff00"
// Mask to keep only lower 24 bits
const masked24 = num.maskn(24);
console.log(masked24.toString(16)); // "ff00"
// AND with small number
const andResult = num.andln(0xFF);
console.log(andResult.toString(16)); // "0"
// Practical example: extract RGB components
const color = new BN('0xFF8000', 16); // Orange color
const red = color.shrn(16).andln(0xFF);
const green = color.shrn(8).andln(0xFF);
const blue = color.andln(0xFF);
console.log(`R: ${red}, G: ${green}, B: ${blue}`); // "R: 255, G: 128, B: 0"const BN = require('bn.js');
// Define bit flags
const FLAGS = {
READ: 0,
WRITE: 1,
EXECUTE: 2,
ADMIN: 3
};
let permissions = new BN('0');
// Set flags
permissions.setn(FLAGS.READ, true);
permissions.setn(FLAGS.WRITE, true);
// Test flags
console.log(permissions.testn(FLAGS.READ)); // true
console.log(permissions.testn(FLAGS.EXECUTE)); // false
// Toggle flag
const hasWrite = permissions.testn(FLAGS.WRITE);
permissions.setn(FLAGS.WRITE, !hasWrite);const BN = require('bn.js');
// Extract bit fields from a packed value
const packed = new BN('0x12345678', 16);
// Extract bits 8-15 (middle byte)
const field = packed.shrn(8).maskn(8);
console.log(field.toString(16)); // "56"
// Extract bits 16-31 (upper 16 bits)
const upper = packed.shrn(16);
console.log(upper.toString(16)); // "1234"const BN = require('bn.js');
function isPowerOfTwo(bn) {
if (bn.isZero()) return false;
return bn.and(bn.subn(1)).isZero();
}
function nextPowerOfTwo(bn) {
if (bn.isZero()) return new BN('1');
let power = new BN('1');
while (power.lt(bn)) {
power.ishln(1);
}
return power;
}
console.log(isPowerOfTwo(new BN('8'))); // true
console.log(isPowerOfTwo(new BN('7'))); // false
console.log(nextPowerOfTwo(new BN('7')).toString()); // "8"
console.log(nextPowerOfTwo(new BN('9')).toString()); // "16"const BN = require('bn.js');
function countSetBits(bn) {
let count = 0;
const bits = bn.bitLength();
for (let i = 0; i < bits; i++) {
if (bn.testn(i)) {
count++;
}
}
return count;
}
const num = new BN('0b11010110', 2); // 214
console.log(countSetBits(num)); // 5 (five 1-bits)