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

onbits(varargin)
% Count "on" bits in argument: onbits, offbits, parity, paritybits, bits, bit, set
%*************************************************************************************
% 
%  MATLAB (R) is a trademark of The Mathworks (R) Corporation
% 
%  Function:    onbits
%  Filename:    onbits.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.
%
%  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.
%
%****************************************************************************/

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

Contact us at files@mathworks.com