FAQ: Why aren't boolean and 1 bit unsigned integers treated as the same data type?

14 Ansichten (letzte 30 Tage)
Variables and signals of the boolean data type (called logical in MATLAB) only represent the values 0 and 1.
Variables and signals that are 1 bit unsigned integers also represent only the values 0 and 1.
Furthermore, variables and signals of both types can be represented on ASICs and FPGAs with just one bit.
So why aren't these treated as the same data type?

Akzeptierte Antwort

Andy Bartlett
Andy Bartlett am 2 Nov. 2020
Bearbeitet: Andy Bartlett am 2 Nov. 2020
Casting a numeric data type to boolean data type is very different from casting to any other numeric type. The following figure shows that casting to boolean behaves very differently from cast to an unsiged 1 bit integer. This difference in behavior when casting to the type is the fundamental reason why boolean and 1 bit unsigned are not the same data type.
Figure: Casting values -5:0.25:5 to boolean and to 1 bit unsigned with saturation and wrapping.
For full details of this example, see the attached pdf. Or, read the quick recap below.
The top plot in the figure shows cast to boolean. The output is always 1 except for the one case when the input is 0.
The middle plot shows a saturating cast to 1 bit unsigned integer. The plot is a step function with an output sequence of all 0s followed by a sequence of all 1s. Clearly, this behaves very differently from cast to boolean.
The bottom plot shows a wrapping cast to 1 bit unsigned integer. The plot is periodic with period two. The first half of a period is all 0s, and the second half is all 1s. Clearly, this behaves very differently from cast to boolean.
-Andy

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by