3
我有一個uint16矢量,我需要翻轉每個數字的最後3位。翻轉矢量的最後3位
我已經這樣做了,但我認爲必須有一個更簡單的解決方案來做到這一點。這是我的代碼。
%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);
正如你可以看到有很多簡單的操作行,我不知道是否有一個更有效的方法來做同樣的事情。
哇,它的工作完美。我只有一個問題,爲什麼是7? – 2013-03-14 05:08:02
7是二進制的111,即三個最低有效位:) – 2013-03-14 05:10:25
絕對是我尋找的那種答案,謝謝:) – 2013-03-14 05:12:04