MATLAB Answers


What function returns (as an integer) the number of bits in a data type or class, e.g. returns 16 for 'int16' or 'uint16' variables, 32 (or whatever) for 'float' types, etc.?

Asked by Jim Tonti on 11 Apr 2019
Latest activity Commented on by Jim Tonti on 22 Apr 2019
The MATLAB function intmax() is close, but is only defined for integer types up to 32 bits. The function class() returns type variable type, but metainformation has to be inferred from documentation for the particular version of MATLAB after that.
Is there a function that returns the native bit size (i.e. the number of bits used for internal storage), or even more useful, all metadata about a given variable or class?
An extension to the function might return other metadata about the variable type, e.g. IEEE format, number of bits, signed/unsigned, etc.
Here's how it might work:
A = uint64(1234);
ans =
[nBits, meta] = varinfo(A)
nBits =
meta =
class = 'uint64'
nBits = 64
signed = 0
value = 1234
format = 'IEEE ...
nExp = 0
nMantissa = 64
max = {actual intmax for the type}
min = {actual minimum value > 0}
{and other useful metadata about the variable)
I'm really hoping something like this already exists...

  1 Comment

The whos function is probably as close as you can currently get to what you want.

Sign in to comment.

2 Answers

Answer by Stephen Cobeldick on 11 Apr 2019
Edited by Stephen Cobeldick on 11 Apr 2019

>> A = single(0);
>> S = whos('A');
>> S.class
ans = single
>> S.bytes*8
ans = 32
If you have non-scalar arrays to get the bits (or bytes) per element simply divide by the number of elements (does not work for empty arrays), or use cast:
B = cast(0,S.class)
S = whos('B');


Sign in to comment.

Answer by James Tursa
on 11 Apr 2019
Edited by James Tursa
on 11 Apr 2019

I think everything that MATLAB runs on currently uses IEEE single and double floating point formats. And the signed integer formats are two's complement storage. Char is actually two bytes per value. Logical is one byte per value. Differences would be big or little endian. So you could easily write a function that gives all of this info, but most of the results would be the same across all platforms for a given class.
What would you use such a function for?


computer() with three outputs outputs the Endian information. Which will be L for all x86 and x64 architectures, which is the only thing that has been supported since R2010a.
Watler- Brilliant! I had forgotten about endian-ness as a structure element for my 'answer'.
James- The function would be used to know 'for sure' how data is represented down to the bit level and have the software adapt or check. Assumptions on data storage are often a major source of problems when older code is ported to newer versions of MATLAB and newer hardware platforms, or needs to run on different platforms. Some day 64-bit or 128-bit ints may be the norm, with other formats following. Re "Since R2010a"... I have MATLAB scripts in use from the late 90's as part of my personal 'toolbox'. Yes, the 15-year old code still works - as long as assumptions aren't violated. Folks who have been coding for more than a decade or so know that when variable sizes, endianness, definitions, etc. change, stuff that depends on the coder looking up parameters in the documentation and monkeying with variables at the bit level will most likely break. This is true in every language I've ever used from assembler on up that allows bit manipulation. As a fair amount of our code works on bits, having a uniformly coded way to adapt to (or at least detect!) changes can be invaluable.

Sign in to comment.