I want to bit right shift logical a hex value.
AF is input hex .Then bit is 10101111 . So bitsrl by 2 should give 101011. So out hex 2B.
I tried converting to dec and binary.But,not getting correct answer.
When converted k0=k0(:) and bit shifted ,I get both A and F shifted by 2.

댓글 수: 1

Jan
Jan 2018년 1월 22일
The question is not clear. What does "bit right shift logical a hex value" mean? Please post the code to define the inputs.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2018년 1월 22일

1 개 추천

With some guessing:
In = 'AF'
a = sscanf(In, '%X');
b = bitshift(a, -2);
Out = sprintf('%X', b)

댓글 수: 2

Adarsh Santhosh
Adarsh Santhosh 2018년 1월 22일
편집: Adarsh Santhosh 2018년 1월 22일
If my In='92f09952c625e3e9' (which is 64 bit) and right shift logical by 63
Out should be 1, right? But,I'm getting 0 by following your suggestion.
Jan
Jan 2018년 1월 22일
You can check this by your own. Don't be shy.
In your original question you wrote 'AF', not '92f09952c625e3e9'. This is an important difference. Check what my code does:
In = '92f09952c625e3e9'
a = sscanf(In, '%x')
>> a: 4294967295
Now you claim that this is 64 bit, but obviously Matlab's sscanf used 32 bits only: a is 2^32-1. See:
dec2bin(a, 64)
>> 0000000000000000000000000000000011111111111111111111111111111111
sscanf('%x') is much faster than hex2dec, but it handles hex values with up to 32 bit only. You could write a work-around, but hex2dec is easier. New version of my code:
In = '92f09952c625e3e9'
a = hex2dec(In)
dec2bin(a) % see below
b = bitshift(a, -2)
Out = dec2hex(b)
But e.g. dec2hex(a) would show a warning. Warnings are important. So let's examine it:
Warning: At least one of the input numbers is larger than
FLINTMAX. Results may be unpredictable.
What is FLINTMAX? The documentation of dec2hex does not explain this, but doc flintmax does. It mentions that the input is larger than 2^53, the maximum number, which can be stored exactly is a double. hex2dec(In) stores the output in a double and this means, that only 53 bits are stored, not 64. The consequence is: hex2dec is not working reliable for 64 bit hexadecimals also.
If you really need a 64 bit hex conversion, you have to write it by your own. This might be useful:
v = sscanf(h, '%2x'); % Read it in 8 bit blocks
and after padding with zeros use typecast(uint8(v), 'uint64'). This is not trivial, but I leave it up to you, because it is not clear what you exactly need.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2018년 1월 22일

댓글:

Jan
2018년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by