Navigation bar
  Start Previous page
 86 of 158 
Next page End  

80
3.24 Sub-byte Types
data types.  These are referred to as sub-byte types because they occupy less than a whole byte – 1 bit
and 4 bits, respectively.  In certain circumstances, these types may help reduce the total RAM usage of
an application.  As compared to packing and unpacking bits in your source code, using these types will be
more efficient in both execution time and code space.
Bit and Nibble types may be used in most places where one of the fundamental data types may be
used.  You can define arrays of them, you can define Bit and Nibble constants, you can pass them as
parameters and you can define functions returning these types.  One limitation is that you cannot pass a
Bit or Nibble variable to a subroutine/function by reference unless the variable is byte-aligned (see
further discussion below).  This implies, of course, that you may not be able to pass an array of sub-byte
types as a parameter since arrays are always passed by reference.  The reason for this limitation is that
sub-byte variables do not necessarily begin on a byte boundary and there is no way for the called routine
to know what the alignment might be.  One way to work around this limitation is to define an integral-byte
alias to the sub-byte type, pass the alias by reference and then define a new sub-byte alias in the called
routine.  This works because integral-byte aliases are required to be byte aligned.  Note that when
passed by value as a parameter, a sub-byte type parameter occupies an entire byte on the stack.
Special type conversion functions, CBit() and CNibble() are provided to facilitate the use of these
types in combination with other types.  See the ZBasic System Library Reference Manual for more
information on the conversion functions.
In Section 3.21 Aliases, it was mentioned that there may an issue with aliases of a fundamental type
overlaying sub-byte types.  The issue arises because fundamental data types must be byte-aligned and a
particular Bit or Nibble variable may or may not be byte-aligned.  In most cases it will be simpler to
define a sub-byte alias to overlay variables of fundamental type.  Doing the converse may require some
experimentation to achieve the required byte-alignment.  The alias defined below may or may not be
accepted by the compiler depending on what other sub-byte types are defined at the same scope level
and the order in which the are defined.
Dim ba(1 to 20) as Bit
Dim bval as Byte Alias ba(3)
Internally, the compiler collects together all of the sub-byte types at a given scope level (module,
procedure, block) and allocates space for them collectively.  The Nibble variables are allocated first in
the order that they are defined followed by the Bit variables in the order that they are defined.  The map
file will show a special variable with a name like @BitNib00.  This is the host variable that contains the
individual Bit and Nibble values for a particular scope level.
BasicX Compatibility Note
Bit and Nibble types are not available in BasicX compatibility mode.
3.24.1 Forcing Byte Alignment
As described earlier, sub-byte type variables are normally sub-allocated within a host variable.  One
consequence of this space-saving strategy is that a sub-byte type variable is often not aligned on a byte
boundary, a circumstance that imposes limitations on the possible uses of the variable.  One solution to
this problem is to instruct the compiler to align a particular variable on a byte boundary.  This is done by
adding the ByteAlign attribute to the variable definition as shown below.
Dim ByteAlign ba(1 to 20) as Bit
Previous page Top Next page