# Section 8.2.1.1:
$BIT_PATTERN

###### abstract class $BIT_PATTERN < $IS_EQ, $IMMUTABLE, $HASH

#### Formal Definitions

This abstract class defines a state component which is a set of all instantiations of objects of any class sub-typing from this class in addition to the vdm model types used wherever this class name is used. Note that SAME has to be an instantiated class, not an abstract one.

types
SAME = object_type ;
$BIT_PATTERN = set of object_type
state
multi : $BIT_PATTERN
inv multi_types ==
forall obj in set multi_types & sub_type($BIT_PATTERN,obj)

NOTE | See the important note about vdm state in the notes on vdm-sl usage in this specification.

This abstract class models the concept of an object to which no semantics is attached, other than bit-pattern equality. Features are specified to manipulate objects as a whole and to modify individual bits - as well as to convert to a numeric value.

#### asize

Since the implementations of classes sub-typing from this abstraction necessarily use the pervasive AVAL constructor class, this reader routine must be defined - to indicate the number of contiguous bits of which an object of the class consists.

asize : CARD

#### null

This feature provides an 'empty' object of asize bits all of which have the value clearbit. Where an object with all bits set is desired then this feature and the bit_invert one defined below could be used.

null | : SAME

##### Formal Signature

null() res : SAME

##### Pre-condition

Since this is a creation operation the pre-condition is vacuously true.

##### Post-condition

post forall elem in set elems res
elem = clearbit

This feature returns an object all of the bits of which have the value clearbit.

#### octet

This feature is a potentially narrowing operation in which only the asize bits with the lowest indices in the sequence are placed into the value returned. The bit-pattern is identical to the bit-pattern in self for those eight bits.

octet | : OCTET

##### Formal Signature

octet(self : SAME) res : OCTET

##### Pre-condition

Since the definition of this feature deliberately limits the range of the result, the pre-condition is vacuously true.

##### Post-condition

post forall index in set inds res
self(index) = res(index)

This feature returns the octet which contains the eight bits of self with the indices 0 to 7.

#### hextet

This feature may be either narrowing or widening, unused bits being ignored if narrowing and additional bits being clearbit if widening.

hextet | : HEXTET

##### Formal Signature

hextet(self : SAME) res : HEXTET

##### Pre-condition

Since the definition of this feature deliberately limits the range of the result, the pre-condition is vacuously true.

##### Post-condition

post let bits : seq of BIT be st bits =
cases self :
(len self > 16) -> forall index in set inds bits
self(index) = bits(index),
(len self = 16) -> self,
(len self < 16) -> self ^ null()(asize + 1, ..., 16) in
res = bits

This feature returns the hextet object which contains the lowest sixteen bits of self - extended with clear bits if self does not contain sixteen bits - with the indices 0 to 15.

#### quad

This feature may be either narrowing or widening, unused bits being ignored if narrowing and additional bits being clearbit if widening.

quad | : QUADBITS

##### Formal Signature

quad(self : SAME) res : QUADBITS

##### Pre-condition

Since the definition of this feature deliberately limits the range of the result, the pre-condition is vacuously true.

##### Post-condition

post let bits : seq of BIT be st bits =
cases self :
(len self > 32) -> forall index in set inds bits
self(index) = bits(index),
(len self = 32) -> self,
(len self < 32) -> self ^ null()(asize + 1, ..., 32) in
res = bits

This feature returns the thirty-two bit wide object which contains the lowest thirty-two bits of self - extended with clear bits if self does not contain thirty-two bits - with the indices 0 to 31.

#### card

This conversion routine to a cardinal number takes the number of bits required to form a cardinal number (or all bits if less than that number of bits) and returns the number formed.

card | : CARD

##### Formal Signature

card(self : SAME) res : CARD

##### Pre-condition

Since the definition of this feature deliberately limits the range of the result, the pre-condition is vacuously true.

##### Post-condition

Note that the value num_bits is that value defined by the implementation in accordance with Section 4.2 of this specification.

post let bits : seq of BIT be st bits =
cases self :
(len self > num_bits) -> forall index in set inds bits
self(index) = bits(index),
(len self = num_bits) -> self,
(len self < num_bits) -> self ^ null(asize + 1, ..., num_bits) in
res = loc_res

This feature returns the cardinal number formed by taking the bit-pattern of self in the lowest indexed bit positions in the result extended with clear bits to make up the number of bits required in a cardinal number.

#### char

This conversion routine to a character uses up to 31 bits from self (see ISO/IEC 10646-1:2000 referred to in Section 2 for the rationale behind this).

char | : CHAR

##### Formal Signature

char(self : SAME) res : CHAR

##### Pre-condition

pre let maxcode = UNICODE.card(UNICODE.Invalid) in
card(self) < maxcode

##### Post-condition

Note that the definition of the class UNICODE gives asize in terms of octets - as per the international standard.

post let maxbits = UNICODE.asize * OCTET.Octet_Bits - 1 in
let bits : seq of BIT =
cases self :
self(index) = bits(index),
(len self = 31) -> self,
(len self < 31) -> self ^ null(asize + 1, ..., 31) in
let loc_res : CHAR be st loc_res = bits in
res = loc_res

This feature returns the bit-pattern of self as an object of type CHAR with which, however, no particular culture-dependent semantics are associated.

#### int

This conversion routine to an integer number takes the number of bits required to form a signed integer number (or all bits if less than that number of bits) and returns the number formed.

int | : INT

##### Formal Signature

int(self : SAME) res : INT

##### Pre-condition

Since the definition of this feature deliberately limits the range of the result, the pre-condition is vacuously true.

##### Post-condition

Note that the value num_bits is that value defined by the implementation in accordance with Section 4.2 of this specification.

post let bits : seq of BIT be st bits =
cases self :
(len self > num_bits) -> forall idx in set inds bits
self(idx) = bits(idx),
(len self = num_bits) -> self,
(len self < num_bits) -> self ^ null(asize + 1, ..., num_bits) in
let loc_res : INT be st loc_res = bits in
res = loc_res

This feature returns a signed integer number formed by taking the bit-pattern of self in the lowest indexed bit positions in the result extended with clear bits to make up the number of bits required in an integer number.

#### bit

Since there is no iterator over individual bits, this routine provides the facility for obtaining individual bit values.

bit | (
| index : CARD
| ) : BIT

##### Formal Signature

bit(self : SAME, index : CARD) res : BIT

##### Pre-condition

preindex < asize

##### Post-condition

In the following it should be remembered that Sather indices start at zero while indices for vdm sequences start at one.

post res = bits(index + 1)

This feature returns the value of the bit indexed by the given parameter.

#### alter

Since there is no iterator over individual bits, this routine provides the facility for setting individual bit values.

alter | (
| index : CARD,
| val : BIT
| ) : SAME

##### Formal Signature

alter(self : SAME, index : CARD, val : BIT) res : SAME

##### Pre-condition

preindex < asize

##### Post-condition

In the following it should be remembered that Sather indices start at zero while indices for vdm sequences start at one.

post forall idx in set {1, ..., index, index + 2, ..., (len bits)}
self(idx) = res(idx)
and res(index + 1) = val

This feature returns a new object containing the same bit-pattern as self except for the one indexed by the first argument which has been set to val.

#### set

This is an indexed version of the similarly named predicate in the pervasive class BIT, returning true if the relevant bit is set.

set | (
| index : CARD
| ) : BOOL

##### Formal Signature

set(self : SAME,index : CARD) res : BOOL

##### Pre-condition

pre index < asize

##### Post-condition

post res = (self(idx) = setbit)

This predicate returns true if and only if the indexed bit of self has the value setbit and vice versa.

#### clear

This is an indexed version of the similarly named predicate in the pervasive class BIT, returning true if the relevant bit is clear.

clear | (
| index : CARD
| ) : BOOL

##### Formal Signature

clear(self : SAME,index : CARD) res : BOOL

##### Pre-condition

pre index < asize

##### Post-condition

post res = (self(idx) = clearbit)

This predicate returns true if and only if the indexed bit of self has the value clearbit and vice versa.

#### bit_invert

This feature inverts all of the bits of self, setbits becoming clearbits and vice versa.

bit_invert | : SAME

##### Formal Signature

bit_invert(self : SAME) res : SAME

##### Pre-condition

Since the domain of the argument and the domain of the result are identical, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
((res(idx) = setbit)
and (self(idx) = clearbit))
or ((res(idx) = clearbit)
and (self(idx) = setbit))

This feature returns the result of setting each bit of self which has the value setbit to clearbit and each bit of self which has the value clearbit to setbit.

#### bit_and

This feature is the classic bit manipulation operation in which each result bit is set only if the corresponding bits of both self and other are set.

bit_and | (
| other : SAME
| ) : SAME

##### Formal Signature

bit_and(self : SAME, other : SAME) res : SAME

##### Pre-condition

Since the range of each individual bit result and the domain of the arguments are the same, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
((self(idx) = setbit)
and (other(idx) = setbit)
and (res(idx) = setbit)) or (res(idx) = clearbit)

This feature returns the result of applying the logical and operation to each individual bit of self and the corresponding bit of other. If both corresponding bits have the value setbit then the corresponding bit of the result will be setbit, otherwise clearbit.

#### bit_or

This feature is the classic bit manipulation operation in which each result bit is set if either of the corresponding bits of self and other are set.

bit_or | (
| other : SAME
| ) : SAME

##### Formal Signature

bit_or(self : SAME, other : SAME) res : SAME

##### Pre-condition

Since the range of each individual bit result and the domain of the arguments are the same, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
(((self(idx) = setbit)
or (other(idx) = setbit))
and (res(idx) = setbit)) or (res(idx) = clearbit)

This feature returns the result of applying the logical or operation to each individual bit of self and the corresponding bit of other. If either corresponding bits have the value setbit then the corresponding bit of the result will be setbit, otherwise clearbit.

#### bit_xor

This feature is the classic bit manipulation operation in which each result bit is set only if the corresponding bit of self and other have different values.

bit_xor | (
| other : SAME
| ) : SAME

##### Formal Signature

bit_xor(self : SAME, other : SAME) res : SAME

##### Pre-condition

Since the range of each individual bit result and the domain of the arguments are the same, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
((((self(idx) = setbit)
and (other(idx) = clearbit))
or ((self(idx) = clearbit)
and (other(idx) = setbit)))
and (res(idx) = setbit)) or (res(idx) = clearbit)

This feature returns the result of applying the logical xor operation to each individual bit of self and the corresponding bit of other. If both corresponding bits have different values then the corresponding bit of the result will be setbit, otherwise clearbit.

#### left

This feature is a 'logical' left shift of the bits in self.

left | (
| places : CARD
| ) : SAME

##### Formal Signature

left(self : SAME, places : CARD) res : SAME

##### Pre-condition

pre places <= asize

##### Post-condition

post ((places = asize)
and (res = null))
or let used = self(places + 1, ..., asize),
empty = null(1, ..., places) in
res = empty ^ used

This feature returns the object which results from shifting the bit-pattern of self left (towards higher indices) by the number of places indicated, filling vacated positions by the value clearbit.

#### right

This feature is a 'logical' right shift of the bits in self.

right | (
| places : CARD
| ) : SAME

##### Formal Signature

left(self : SAME, places : CARD) res : SAME

##### Pre-condition

pre places <= asize

##### Post-condition

post ((places = asize)
and (res = null))
or let used = self(places + 1, ..., asize),
empty = null(1, ..., places) in
res = used ^ empty

This feature returns the object which results from shifting the bit-pattern of self right (towards lower indices) by the number of places indicated, filling vacated positions by the value clearbit.

#### highest

This feature returns the index of the highest bit of self which has the given value (either setbit or clearbit).

highest | (
| val : BIT
| ) : CARD

##### Formal Signature

highest(self : SAME, val : BIT) res : CARD

##### Pre-condition

Since the domain of the result is greater than the domain of indices of self the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
self(idx) <> val
and (res = CARD.maxval))
or ((self(res + 1) = val)
and ((res = (asize - 1))
or (forall idx in set (inds self)(res + 2, ..., asize) &
self(idx) <> val

This feature returns the index of the highest bit which has the value given by the argument - or CARD::maxval if no bit has that value.

#### highest

This feature returns the index of the highest bit of self which has the value setbit.

highest | : CARD

##### Formal Signature

Note that the name here has been changed since name overloading is not possible in vdm.

highest2(self : SAME) res : CARD

##### Pre-condition

Since the domain of the result is greater than the domain of indices of self, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
self(idx) <> setbit
and (res = CARD.maxval))
or ((self(res + 1) = setbit)
and ((res = (asize - 1))
or (forall idx in set (inds self)(res + 2, ..., asize) &
self(idx) <> setbit

This feature returns the index of the highest bit which has the value setbit - or CARD::maxval if no bit is set.

#### lowest

This feature returns the index of the lowest bit of self which has the given value (either setbit or clearbit).

lowest | (
| val : BIT
| ) : CARD

##### Formal Signature

lowest(self : SAME, val : BIT) res : CARD

##### Pre-condition

Since the domain of the result is greater than the domain of indices of self, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
self(idx) <> val
and (res = CARD.maxval))
or ((self(res + 1) = val)
and ((res = 0))
or (forall idx in set (inds self)(1, ..., res) &
self(idx) <> val

This feature returns the index of the lowest bit which has the value given by the argument - or CARD::maxval if no bit has that value.

#### lowest

This feature returns the index of the lowest bit of self which has the value setbit.

lowest | : CARD

##### Formal Signature

Note that the name here has been changed since name overloading is not possible in vdm.

lowest2(self : SAME) res : CARD

##### Pre-condition

Since the domain of the result is greater than the domain of indices of self, the pre-condition is vacuously true.

##### Post-condition

post forall idx in set inds self &
self(idx) <> setbit
and (res = CARD.maxval))
or ((self(res + 1) = setbit)
and ((res = 0)
or (forall idx in set (inds self)(1, ..., res) &
self(idx) <> setbit

This feature returns the index of the lowest bit which has the value setbit - or CARD::maxval if no bit is set.

#### hex_str

This is the first of two features of this name which return a text string representation of the bit-pattern as a sequence of hexa-decimal digits.

hex_str | (
| lib : LIBCHARS
| ) : STR

##### Formal Signature

hex_str(self : SAME, lib : LIBCHARS) res : STR

##### Pre-condition

Since the domain of self is smaller than the domain for the result and the lib argument is not an optional type, the pre-condition is vacuously true.

##### Post-condition

post let str_size = (asize + 1) mod 4 in
forall idx in set inds res &
LIBCHARS.hex_card(lib,res(idx)) =
let bitidx = (idx - 1) * 4 + 1 in
card(self(bitidx, ..., bitidx + 3))

This feature returns a hexadecimal text string representation of self using the given encoding and repertoire. Each character of the string is a hexadecimal digit for a corresponding sub-sequence of four bits from self. No prefix or suffix of any kind is to be included.

#### hex_str

This second string representation feature uses the execution environment default culture, encoding and repertoire to determine the actual representation characters.

hex_str | : STR

##### Formal Signature

Note that the name of this formal signature has been changed since there is no name overloading in vdm.

hex_str2(self : SAME, lib : LIBCHARS) res : STR

##### Pre-condition

Since the domain of self is smaller than the domain for the result and the lib argument is not an optional type, the pre-condition is vacuously true.

##### Post-condition

post let str_size = (asize + 1) mod 4 in
forall idx in set inds res &
LIBCHARS.hex_card(LIBCHARS.default,res(idx)) =
let bitidx = (idx - 1) * 4 + 1 in
card(self(bitidx, ..., bitidx + 3))

This feature returns a hexadecimal text string representation of self using the default encoding and repertoire. Each character of the string is a hexadecimal digit for a corresponding sub-sequence of four bits from self. No prefix or suffix of any kind is to be included.

| Language Index |  | Library Index |  | Binary Index

Comments or enquiries should be made to Keith Hopper. Page last modified: Wednesday, 22 November 2000. |