3

すべての数値の最後の 3 ビットを反転する必要がある uint16 ベクトルがあります。

私はすでにこれを行っていますが、これを行うためのより簡単な解決策が必要だと思います。これが私のコードです。

%Turn the vector to binary
V_bin = dec2bin(V,16);
for i=1:length(V)
  %Get the last 3 bits
  tmp = V_bin(14:end);
  %Convert the string to decimal
  tmpdec = bin2dec(tmp);
  %Do the flip
  tmpflip = bitcmp(uint8(tmpdec));
  %Flipped to binary
  tmpbin = dec2bin(tmpflip);
  %Replace the flipped bits in the original string
  V_bin(14:end) = tmpbin(6:end);
end
V = bin2dec(V_bin);

ご覧のとおり、単純な操作には多くの行がありますが、同じことを行うためのより効率的な方法があるのではないかと思います。

4

1 に答える 1

5

私はmatlabに精通していませんが、bitxor関数はあなたに適しているようです.

V = bitxor(V, 7);
于 2013-03-14T04:57:51.940 に答える