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 Comment

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

Sign in to comment.

 Accepted Answer

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

2 Comments

Adarsh Santhosh
Adarsh Santhosh on 22 Jan 2018
Edited: Adarsh Santhosh on 22 Jan 2018
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.
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.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 22 Jan 2018

Commented:

Jan
on 22 Jan 2018

Community Treasure Hunt

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

Start Hunting!