TapHome

Bit operations

TapHome script bit and byte manipulation — GETBIT, GETBITS, GETBYTE, SETBYTE, SETBIT, SETBITS, bit shifts, and bitwise AND/OR operators.

GETBIT

Returns a value of a bit in the specified position.

GETBIT(number, bit_position)

Arguments:

  • number - number to extract value of specific bit from
  • bit_position - position of bit, starting with 0, from right

Examples:

  • GETBIT(2, 0) → first bit of number 2 (0b0010) is 0
  • GETBIT(4,2) → third bit of number 4 (0b0100) is 1

GETBITS

Returns value of specified number of bits in the specified position.

GETBITS(number, start_bit, number_of_bits)

Examples:

  • GETBITS(216, 3, 2) → number 216 = 0b1101 1000; value of 4th bit from the right is 1, 5th bit is 1, therefore result is 0b0011 = 3
  • GETBITS(0xFF, 0, 4) → number 0xFF = 255 = 0b1111 1111; value of first 4 bits from right is 0b1111 = 0xF = 15

GETBYTE

Returns a value of a byte in the specified number.

GETBYTE( number, byte_position )

Arguments:

  • number - number to extract value of specific byte from
  • byte_position - position of byte, starting from 0, from right

Examples:

GETBYTE(256, 0)  →  0001 0000 0000 →  0
GETBYTE(256, 1)   →  0001 0000 0000  →  1
GETBYTE(259, 0)   →  0001 0000 0011   →  3

SETBYTE

Assigns a new value to the specified byte in the provided number, and returns assigned value.

SETBYTE( number, byte_position, new_value )

Examples:

SETBYTE(1, 0, 0)   →   0
SETBYTE(256, 0, 255)   →   511
SETBYTE(256, 1, 1)	  →   256
SETBYTE(259, 1, 2)	  →    515

SETBIT

Assigns a new value to the specified bit in the provided number and returns a new number.

SETBIT(number, bit_position, new_value)

Arguments:

  • number - number to be modified
  • bit_position - position of bit, starting with 0, from right
  • new_value - 0 or 1 - value that is going to be set to specified bit

Examples:

  • SETBIT(1, 1, 1) → 3
  • SETBIT(3, 1, 1) → 3
  • SETBIT(4, 2, 0) → 4
  • SETBIT(12, 1, 0) → 14

SETBITS

Assigns a new value to the specified bits in the provided number and returns a new number.

SETBITS(number, start_bit, number_of_bits, new_value)

Examples:

  • SETBITS(192, 4, 2, 3) → 240
  • SETBITS(192, 5, 2, 3) → 224

«   (LEFT BIT SHIFT)

8 << 2   (32)

Excel: BITLSHIFT(number, shift_amount)

» (RIGHT BIT SHIFT)

32 >> 2   (8)

Excel: BITRSHIFT(number, shift_amount)

& (BITWISE AND)

3 & 1   (1)

Excel: BITAND(number1, number2)

| (BITWISE OR)

2 | 1 (3)

Excel: BITOR(number1, number2)

See the example of bit operations in Google Sheets: https://docs.google.com/spreadsheets/d/1hF5FMpGMJbgYh-YLwWrq2n186_ATyGyLUb689__IhLY/edit?usp=sharing

Or try interactive tool at http://bitwisecmd.com/