## Count "on" "off" or calculate simple "parity" bits of input

version 1.0.0.0 (12.3 KB) by
Three functions to calculate the number of "on", "off", or simple "parity" bits of input

Updated Mon, 11 Aug 2008 13:19:13 +0000

Important: You don't have to know anything about mex or c to use this file! Simply follow the instructions below to build the mex files.

Three mex functions are supplied:

onbits is a mex function that counts the number of "on" bits in variable elements. Returns a variable with the same size & structure as the input. Fundamental types (double, single, int8, etc) are replaced with a int8 variable, each element being the number of "on" bits for the corresponding input variable element. For structures and cell arrays, each component or cell is individually counted. Other classes (e.g., vpa, function handles, etc.) are not counted, the return variable is empty. The complex portion of variables are counted as well. Can take any number of inputs as long as there are matching outputs. Valid variable types for input are:

double
single
uint64
int64
uint32
int32
uint16
int16
uint8
int8
char
logical
cell
structure

Building:

>> mex -setup
>> mex onbits.c

Syntax:

Y = onbits(X)
[Y1 Y2] = onbits(X1,X2)
[Y1 Y2 Y3] = onbits(X1,X2,X3)
: :
etc etc

Note:

For single and double type inputs, onbits counts the actual bits in the floating point representation of the number. onbits does *not* count the bits in the equivalent integer representation of the number. For example:

onbits(int8(-1)) will give a result of 8 (all bits set "on"), whereas onbits(-1) will give a result of 11, since that is the number of "on" bits in the double floating point representation of -1.

--------------------

offbits is similar to onbits, but counts the number of "off" bits instead of "on" bits.

Building:

>> mex -setup
>> mex offbits.c

Syntax:

Y = offbits(X)
[Y1 Y2] = offbits(X1,X2)
[Y1 Y2 Y3] = offbits(X1,X2,X3)
: :
etc etc

--------------------

paritybits is similar to the previous two functions, but calculates simple even or odd parity.

Building:

>> mex -setup
>> mex paritybits .c

Syntax:

Y = paritybits (X)
[Y1 Y2] = paritybits (X1,X2)
[Y1 Y2 Y3] = paritybits (X1,X2,X3)
: :
etc etc

### Cite As

James Tursa (2022). Count "on" "off" or calculate simple "parity" bits of input (https://www.mathworks.com/matlabcentral/fileexchange/21025-count-on-off-or-calculate-simple-parity-bits-of-input), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2007a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux