Learning BSV‎ > ‎Data Types‎ > ‎

Bool

The Bool type is defined to have two values, True and False.
 
typedef enum {False, True} Bool;

Usage

  • Bool defines a Boolean variable.
  • The valid values are True or False. 
  • The implementation is one bit.  Bool is used for True or False values, Bit#(1) is used for variables with values 0 or 1.
  • You cannot automatically convert from a Bool to a Bit#(1).
  • Bit-wise operations on a Bool are illegal.

Functions

The following Bool functions return either a value of True and False.
  • not or !
    • Returns  True if x is false, returns False if x is true
  • &&
    •  Returns True if x and y are true, else it returns False.
  • ||
    • Returns True if x or y are true, else it returns False.

Examples

  Bool  a             // A variable named a with a type of Bool.  Valid Values are True or False
 Reg#(Bool) done     // A register named done with a type of Bool
 Vector#(5, Bool)    // A vector of 5 Boolean values

 Bool  a = 0;     // ERROR!  You cannot do this
 Bool  a = True;  // correct
 if (a)           // correct
   ....
 if (a == 0)      // ERROR!
   ....

Data Type Conversions

Converting between Bool and Bit#(1)

Warning:  If you are performing many conversions from Bit#(1) to Bool you probably should redefine some of your Bits as Bools.

 Bool b = (z == 0);      // Convert from bit to Bool
 bit  x = b ? 1 : 0;     // Convert from Bool to bit

 Bit#(1) a = 0;
 Bool b =  unpack (a);   //Convert from bit to Bool
 a = pack (b);           //Convert from Bool to bit


Data Type Conversion Functions

Bluespec provides a set of functions to convert a type between Bool and another type.  During type checking, the compiler resolves these functions to a particular type instance. If you have excessive type conversion in your design, it usually indicates a poor choice of the basic object types in the design, and you may want to review your type choices.  These conversion utilities do not incur logic overhead.

Comments