Plug4
Plug4

Reputation: 3928

MATLAB: logic behind bitand and get all possible states

I was provided with this code

m0=0.8;
m1=1.2;
k=6; %where k can take values between 2 and 10;
kbar=2^k;
g_m = [0:(kbar-1)];

for i = 1: (kbar)
  g=1;
  for j=0:(kbar-1)
    if(bitand(g_m(i),2^j))~=0
      g=g*m1;
    else
      g=g*m0;
    end
  end
  g_m(i)=g %results in a 1xN vector where N = all the possible states
end

My question is why the function of bitand allows you to generate all the possible "states"? I am not too sure if I really understand the logic behind bitand beside searching if the values that it compares have a bit = 1, hence ans=1.

Upvotes: 1

Views: 291

Answers (1)

Radu Pluta
Radu Pluta

Reputation: 131

bitand takes 2 values, converts them into binary, makes logical AND between the two values and returns the result form the logical AND as a decimal number. so for 2 given numbers, it returns only one value

Upvotes: 1

Related Questions