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

offbits(varargin)
% Count "off" bits in argument: onbits, offbits, parity, paritybits, bits, bit, set
%*************************************************************************************
% 
%  MATLAB (R) is a trademark of The Mathworks (R) Corporation
% 
%  Function:    offbits
%  Filename:    offbits.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.
%
%  offbits is a mex function that counts the number of "off" 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 "off" 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 offbits.c
% 
%  Syntax:
% 
%    Y = offbits(X)
%    [Y1 Y2] = offbits(X1,X2)
%    [Y1 Y2 Y3] = offbits(X1,X2,X3)
%        :           :
%       etc         etc
% 
%  Note:
% 
%   For single and double type inputs, offbits counts the actual bits in the
%   floating point representation of the number. offbits does *not* count
%   the bits in the equivalent integer representation of the number. For
%   example:
% 
%   offbits(int8(-1)) will give a result of 0 (no bits set "off"), whereas
%   offbits(-1) will give a result of 53, since that is the number of "off"
%   bits in the double floating point representation of -1.
%
%****************************************************************************/

function varargout = offbits(varargin)
disp('Error using offbits: You have not yet generated the offbits 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 offbits.c');
disp(' ');
disp('That''s it! Now you are ready to use offbits.');
error(' ');
end

Contact us at files@mathworks.com