Experimentation Stage

Binary

A Binary in Lense is a sequence of bits and implements Sequence<Boolean> and may not represent a number. Binary is a fundamental type in Lense so it is specially supported by the language.

Two main implementations of Binary exist : BitList that supports a variable size of bits, and BitArray that supports a fixed length of bits.

Binary Literal Representation

The literal of binary begins with a $ sign followed by a sequence of ones (to represent true) and zeros (to represent false). The _ symbol can be used, as in number literals, to separate digits logically.

	let  byte = $1111_0000;
	let  short = $1111_0000_1111_0000;
	let  flags = $1111_0000_0101_0110_0010_0001_0101_1001;

All binary literals are assumed to be instances of BitArray with a size equivalent to the given number of bits.

Note
It is not possible to represent a zero bits sequence with a literal.

The above code is equivalent to:

	let  byte : Binary = BitArray.valueOf(true,true,true,true, false,false,false,false);
	let  short : Binary = BitArray.valueOf(true,true,true,true, false,false,false,false, true,true,true,true, false,false,false,false);
	let  flags : Binary = BitArray.valueOf(
					true,true,true,true, false,false,false,false,
					false,true,false,true, false,true, true,false,
					false,false,true,false, false,false,false,true,
					false,true,false,true, true, false,false,true
				);
Note
This equivalency is conceptual, in practice, the compiler can optimize the construction of literals.

Binary and Bytes

Byte is a special case of Binary that corresponds to a fixed sequence length of 8 bits. Byte is primarily intended for use in I/O operations and is not a number. Byte does not have an assigned numeric value and there is no automatic promotion from Byte to any type of Number. Also it has no arithmetic operations. However, a Byte can be transformed explicitly to a Natural between 0 and 255 or to a Int32 between -128 and 127 by means of the toNatural() and toInteger() functions.

	let  byte : Byte = $1111_0000;
	let  n : Natural = byte.toNatural(); // equivalent to 240;
	let  i : Int32 = byte.toInteger(); // equivalent to -16

	let error : Natural = byte; // illegal. Byte is not assignable to Natural.

Int16 , Int32 and Int64 also implement Binary corresponding to a fixed length sequence of 16, 32 and 64 bits respectively. Because this values have a signed numeric value, one of the bits is reserved to determine the sign as numbers. The other bits represent the value if the value is positive, or represent the two’s complement representation of the (then negative) value.

Bitwise Operations

Binary satisfies Injuctable, Dijunctable and ExclusiveDijunctable to allow for the use of the following bitwise operators:

  • & from Injuctable : the bitwise AND operator

  • | from Dijunctable : the bitwise OR operator

  • ^ from ExclusiveDijunctable: the bitwise XOR (Exclusive-OR) operator

These operators are commutative and operate bitwise according to they respective traditional Truth Tables.

` Binary` also satisfies Complementable to allow for the use of the complement operator:

  • ~ from Complementable : the bitwise complement operator.

Note
When the type implementing Binary also is a number like Int32 or Int64 the following relation olds: -x = ~x + 1

Binary additionally defines two other bitwise operators:

  • >> - arithmetic right shift

  • << - arithmetic left shift

These operators are called arithmetic because they are related to the multiplication and division operations

	let number : Int32 = 60;

	assert( number << 2 == 360 ) // equivalent to 360 = 60 * 2 * 2
	assert( number >> 2 == 15 ) // equivalent to 15 = 60 / 2 / 2

Using binary literals:

	let number : Byte = $0011_1100;

	assert( number << 2 == $1111_0000 ) // equivalent to 360 = 60 * 2 * 2
	assert( number >> 2 == $0000_1111 ) // equivalent to 15 = 60 / 2 / 2

Shifting to the left n times is equivalent to multiplying by 2n. On the other hand shifting to the right n types equivalent to integer dividing by 2n. In general right shifting is equivalent to taking the floor of the division.

Note
x << n <⇒ x * 2n and x >> n <⇒ x \ 2n