help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Binary AND


From: Akira Nishimura
Subject: Re: Binary AND
Date: Thu, 07 Oct 1999 15:49:22 +0900

From: Bostjan JERKO <address@hidden>
Subject: Binary AND
Date: 07 Oct 1999 08:30:26 +0200
Message-ID: 
  <071ED37FC3E02040*/c=FR/admd=ATLAS/prmd=SG/o=INFI/s=JERKO/g=BOSTJAN/@MHS>

>Is there a function for binary AND like & in C (in Octave I get error
>message if using e.g. 255 & 127 ) ?

I wrote a bitand function compatible to MATLAB. But I think there
must be more simple way to implement a bitand function by m-file or
built-in function.

function c = bitand(a, b)

% c = bitand(a, b);
%
% returns the bit-wise AND of the two arguments A and B. 
% Both A and B must contain non-negative integers between 0
% and BITMAX.

if a < 0 | b < 0
  error('a and b must be non-negative');
  return;
end
if a > bitmax() | b > bitmax()
  error('a and b must be less than BITMAX');
end

if rem(a, 1) ~= 0 | rem(b, 1) ~= 0
  error('a and b must be integers');
  return;
end

an = nextpow2(a);
bn = nextpow2(b);

mm = bn;
if an < bn
  mm = an;
end

c = 0;
for k=0:mm
  if(rem(a, 2) == rem(b, 2) & rem(b, 2) == 1)
    c = c + 2^k;
  end
  a = floor(a/2);
  b = floor(b/2);
end

function c = bitmax()

c = 2/eps;

_/_/_/   _/  _/            _/            _/_/        Akira NISHIMURA
 _/     _/  _/            _/             |    Dept. of Information Systems
_/okyo  _/_/niversity of _/nformation _/_/ciences address@hidden
                   http://www.rsch.tuis.ac.jp/~akira



---------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.  To ensure
that development continues, see www.che.wisc.edu/octave/giftform.html
Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
---------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]