# Bitstrings

A bitstring is another special kind of string, made up of a compressed series of bits. A 32K variable can hold almost 256K bits. There are special functions for creating and working with bitstrings.

• \$Bit retrieves or sets (used with Set) a bit to 1 or 0. The examples below use \$Random(2) to randomly generate either a 1 or a 0.

• \$BitCount counts bits.

• \$BitFind finds the next bit with a specific value (1 or 0).

• \$BitLogic performs bit-wise operations (and, or, not, xor) on bitstrings.

• \$Factor converts an integer to the bitstring representing it. For example, \$Factor(4) returns the bitstring 001.

Since the bitstring is compressed, you shouldn't Write it—either use \$Bit, or you can use the Zwrite command, which shows both the byte and bit contents.

SAMPLES>for i=1:1:40 {set \$bit(b, i) = \$random(2)}

SAMPLES>write \$bitcount(b, 0)
23
SAMPLES>write \$bitcount(b, 1)
17
SAMPLES>for i = 1:1:40 {write \$bit(b, i)}
0100110001000110101011001100010010000111
SAMPLES>write \$bitfind(b, 1, 1) // find the first 1 bit
2
SAMPLES>write \$bitfind(b, 0, 1) // find the first 0 bit
1
SAMPLES>write \$bitfind(b, 1, 3) // find the next 1 bit
5
SAMPLES>for i = 1:1:40 {set \$bit(c, i) = \$random(2)}

SAMPLES>set d = \$bitlogic(b & c)

SAMPLES>for i = 1:1:40 {write \$bit(b, i)}
0100110001000110101011001100010010000111
SAMPLES>for i = 1:1:40 {write \$bit(c, i)}
0111001111001011110100100110101110111011
SAMPLES>for i = 1:1:40 {write \$bit(d, i)}
0100000001000010100000000100000010000011
SAMPLES>set f = \$factor(23456)

SAMPLES>zwrite f  // shows the bitstring representing 23456
f=\$c(128,0,4,0)_" ["_\$c(0,0)/*\$bit(000001011101101)*/

FeedbackOpens in a new tab