No BSD License  

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

image thumbnail
from Count "on", "off", or calculate simple "parity" bits of input by James Tursa
Three functions to calculate the number of "on", "off", or simple "parity" bits of input

paritybits(varargin)
% Count "parity" bit of argument: onbits, offbits, parity, paritybits, bits, bit, set
%*************************************************************************************
% 
%  MATLAB (R) is a trademark of The Mathworks (R) Corporation
% 
%  Function:    parity
%  Filename:    parity.c
%  Programmer:  James Tursa
%  Version:     1.0
%  Date:        August 4, 2008
%  Copyright:   (c) 2008 by James Tursa
%  Permission:  Permission is granted to freely distribute and use this code
%               as long as the header information is included.
% 
%  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 file.
%
%  paritybits is a mex function that calculates the parity bit 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 parity bit for the corresponding input
%  variable element. For structures and cell arrays, each component or cell
%  is individually calculated. Other classes (e.g., vpa, function handles,
%  etc.) are not counted, the return variable is empty. The complex portion
%  of variables are calculated 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 paritybits.c
% 
%  Syntax:
% 
%    Y = paritybits(X)
%    [Y1 Y2] = paritybits(X1,X2)
%    [Y1 Y2 Y3] = paritybits(X1,X2,X3)
%        :           :
%       etc         etc
% 
%  Note:
% 
%   For single and double type inputs, paritybits calculates using the
%   actual bits in the floating point representation of the number.
%   paritybits does *not* calculate using the equivalent integer
%   representation of the number. For example:
% 
%   paritybits(int8(-1)) will give a result of 0 (even parity), whereas
%   paritybits(-1) will give a result of 1, since the number of "on" bits
%   in the double floating point representation of -1 is 11, an odd number.
%
%****************************************************************************/

function varargout = paritybits(varargin)
disp('Error using offbits: You have not yet generated the paritybits mex routine.');
disp('Do the following:');
disp(' ');
disp('>> mex -setup');
disp('  (Then follow instructins to select the C compiler of your choice. (e.g., lcc)');
disp('>> mex paritybits.c');
disp(' ');
disp('That''s it! Now you are ready to use paritybits.');
error(' ');
end

Contact us at files@mathworks.com