Skip to main content

BITSET

Sets the specified bit in a bitstring to 1.

Synopsis

BITSET(bitstring,bitno)

Arguments

bitstring The bit string, specified as an expression that resolves to a positive integer. For example, the integer 64 specifies the bitstring 1000000. The maximum bitstring value is 9223372036854775807.
bitno The bit position in bitstring to set to 1. An expression that resolves to a positive integer. Bit positions are counted right to left, beginning with position 0. The maximum bitno value is 62. A fractional bitno is truncated to its integer portion. A negative bitno generates a <FUNCTION> error.

Description

The BITSET function sets a single bit of bitstring to 1 at the bit location specified by bitno. Both values are specified as positive integers. bitno always sets the specified bit to 1. If the bit specified by bitno has a value of 0, BITSET sets it to 1. If the bit specified by bitno already has a value of 1, BITSET sets it to 1 (leaves it unchanged).

Both bitstring and bitno can be expressed as either numbers or as strings. These numbers are converted to canonical form, with leading plus signs and leading and trailing zeros omitted. If either argument evaluates to the null string or a non-numeric string it is assumed to have a value of 0. A string is parsed as a number until a non-numeric character is encountered. Thus “7dwarves” is parsed as 7.

If bitno is specified as a decimal fraction it is truncated to its integer component.

The BITSET function sets a specified bit to 1. The BITRESET function sets a specified bit to 0. The BITNOT function sets a specified bit to its opposite value.

Examples

The following example specifies a bitstring of either 0 or 1. It then sets the bit position specified in bitno to bit value 1:

PRINT BITSET(0,0);  ! Sets bit position 0 to 1; returns integer 1
PRINT BITSET(0,1);  ! Sets bit position 1 to 1; returns integer 2
PRINT BITSET(1,0);  ! Sets bit position 0 to 1; returns integer 1
PRINT BITSET(1,1);  ! Sets bit position 1 to 1; returns integer 3

The following example specifies a bitstring of 64 (binary 1000000), and bitno sets bit position 0 to the bit value 1. This results in the binary string 1000001, the integer value of which is 65:

PRINT BITSET(64,0);  ! Returns 65

The following example specifies a bitstring of 64 (binary 1000000), and bitno sets bit position 4 to the bit value 1. This results in the binary string 1010000, the integer value of which is 80:

PRINT BITSET(64,4);  ! Returns 80

The following example specifies a bitstring of 65 (binary 1000001), and bitno specifies setting bit position 0 to the bit value 1. But because bit position 0 already has a bit value of 1, the binary string 1000001 (integer value 65) is returned unchanged:

PRINT BITSET(65,0);  ! Returns 65

The following example specifies a bitstring of 8 (binary 1000), and bitno specifies setting bit position 4 to the bit value 1. The bitstring has an implicit bit position of 4 with a value of 0. Setting this bit to 1 returns the binary string 11000, the integer value of which is 24:

PRINT BITSET(8,4);  ! Returns 24

The following example specifies bitstring and bitno with null string values. The null string is parsed as 0:

PRINT BITSET("",1);   ! Returns 2; same as BITSET(0,1)
PRINT BITSET(1,"");   ! Returns 1; same as BITSET(1,0) 
PRINT BITSET("","");  ! Returns 1; same as BITSET(0,0)

See Also

FeedbackOpens in a new tab