No BSD License  

Highlights from
Count "on", "off", or calculate simple "parity" bits of input

Be the first to rate this file! 1 Download (last 30 days) File Size: 12.3 KB File ID: #21025
image thumbnail

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

by James Tursa

 

10 Aug 2008 (Updated 11 Aug 2008)

Three functions to calculate the number of "on", "off", or simple "parity" bits of input

| Watch this File

File Information
Description

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
    (then follow instructions to select a C compiler of your choice)
  >> 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
    (then follow instructions to select a C compiler of your choice)
  >> 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
    (then follow instructions to select a C compiler of your choice)
  >> mex paritybits .c
 
Syntax:
 
    Y = paritybits (X)
    [Y1 Y2] = paritybits (X1,X2)
    [Y1 Y2 Y3] = paritybits (X1,X2,X3)
        : :
       etc etc

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Any C compiler, such as the built-in lcc compiler
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
coding theory James Tursa 22 Oct 2008 10:14:04
information theory James Tursa 22 Oct 2008 10:14:04
onbits James Tursa 22 Oct 2008 10:14:04
offbits James Tursa 22 Oct 2008 10:14:04
paritybits James Tursa 22 Oct 2008 10:14:04
parity James Tursa 22 Oct 2008 10:14:04
bits James Tursa 22 Oct 2008 10:14:05
bit James Tursa 22 Oct 2008 10:14:05

Contact us at files@mathworks.com