How can I apply circular shift on bits in MATLAB?

5 Ansichten (letzte 30 Tage)
Sana Shaikh
Sana Shaikh am 11 Apr. 2013
I want to apply shift right operation on aset of bits

Akzeptierte Antwort

Youssef  Khmou
Youssef Khmou am 12 Apr. 2013
hi,
suppose you have a vector a=011000011, then shift circularly the vector :
b=circshift(a,[1 1]), % if 1 is >0 then the vector is shifted the right
b=circshift(a,[1 -1])
b=circshift(a,[1 2]),
...

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 11 Apr. 2013
MATLAB does not offer that operation directly.
You can extract the bits that would be shifted "off the bottom", do the shift, and then put the bits back on top. Or you can proceed numerically, such as
mod(x,2)*2^7 + floor(x/2)
  7 Kommentare
Walter Roberson
Walter Roberson am 6 Jun. 2017
Do you want the individual bits to be left shifted, or do you want the elements to be left shifted relative to the other elements? For example if you had A = uint8([1 2 3 129]) to be circular left shifted by 2, then do you want the result B = uint8([3 129 1 2]) or do you want the result B = uint8([4, 8, 12, 6]) ?
If you want the elements to be left shifted relative to each other, then
B = circshift(A, -K, 2);
If you want the bits to be circular shifted, then use the code I posted above (which I made a couple of corrections to a moment ago.)
If you want the bits to be circular shifted and you want the operation to be expressed mathematically, it is possible. In the above code, replace
shifted_uMatrix = bitshift(uMatrix, K, uc) + bitshift( uMatrix, K-wl, uc);
with
shifted_uMatrix = mod(uMatrix, 2^(wl-K)) * 2^K + uMatrix ./ 2^(wl-K);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by