How does bitshift work?

3 views (last 30 days)
Kaavya N
Kaavya N on 4 May 2021
Commented: Scott MacKenzie on 5 May 2021
gives answer 28 in matlab
My understanding of this working is as follows:
the function converts 56 to binary value ie 0101 0110 and does right shift since its -1
so 0010 1011 -> 2b (hexa)
so the answer should be "2b" right?
How does this function work?
Kaavya N
Kaavya N on 5 May 2021
Thank you

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 4 May 2021
ans = '00111000'
In other words, your difficulty is that 0101 0110 is not the correct representation for 56.
Scott MacKenzie
Scott MacKenzie on 5 May 2021
It's also worth noting that with each division-by-two the remainder (0 or 1) becomes part of the binary result. The first remainder is the bit on the right (the least-significant bit). Each successive remainder is appended on the left. The following figure might help. It shows the conversion from decimal 26 to the binary equivalent, 11010.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by