Please help me correct or fix my code !!

7 views (last 30 days)
Irwin2020
Irwin2020 on 21 Jul 2020
Commented: Walter Roberson on 31 Jul 2020
Hello Everyone!,
I would really apricate it if you could help me correct the code below to shift a 24 Bit (6 hex char '6BB92C000000') within a 64 bit register '00006BB92C000000''. Notes that I have already offset it the hex char with 2^24 ..i.e Multiplied (''6BB92C) * 2^24.
I'm tryiing to shift this register with the following values : '05', 'F1','07','1A', '2A', and 'EA', any Idea how to get a result without overflow the conversion ? or getting an error or a warning message when I tried the negative hex values ? I know for a fact that, MATLAB dose not support conversion for any bit higher than 53. Also . I know the arithmetic shift of a binary numbers ( Left shift Multiply by 2^nShift & for the Right shift we divide by 2^nShift), any ideas?. I have also tried to convert the Fraction-floting point to decimal & Binarry, and still dese not giving me the correct answer. Thank you and I appreciate your help.
hex= '6BB92C';
hexi2Decimal= hex2dec(hex); % Hexal in Decimal
zeroOffset=dec2hex(hexi2Decimal*2^24) % Offest with 24 zeros to the left
zeroOffsetinBin=dec2bin(hex2dec(zeroOffset)); % Offest with 24 zeros to the left in Binary
zeroShiftinDeca=hex2dec(zeroOffset);
nShift=hex2dec('05') % Number of Shift in hex or dec
ShiftedHexinDecimal= hex2dec(zeroOffset)*2^nShift;
finalShiftedRegister=dec2hex(ShiftedHexinDecimal) % Final shiffted register
ShiftedHexinBin=dec2bin(ShiftedHexinDecimal);
% MATLAB Built-in function for bitshift
BitShift=dec2hex(bitshift(zeroShiftinDeca,nShift))

Answers (1)

Walter Roberson
Walter Roberson on 21 Jul 2020
Use uint64. You can use bitxor, bitand, bitor, bitshift.
If you have an input value expressed in hex, then sscanf with %lx format can read up to 16 nibbles as uint64
  8 Comments
Irwin2020
Irwin2020 on 31 Jul 2020
Thanks for your comments. I’m using One’s Complement only if needed and my system doesn’t require a division, simply I'm trying to avoid any division that involves in my calculations, because it makes thing more complicated. Also , I’m aware of the signed zero implications, all I wanted is that , to bypass the 53 bit issue with MATLAB, specially when we divide and do dec2hex. I picked ‘F0’ as an arbitrary value. all I wanted is to shift left - right without getting any warning or error message by using MATLAB built in functions in my code.
Walter Roberson
Walter Roberson on 31 Jul 2020
> sprintf('%016x', 0x6BB92C000000)
ans =
'00006bb92c000000'
That is, instead of using dec2hex, use sprintf() . And sscanf() for the reverse.
>> sscanf('00006bb92c000000', '%lx')
ans =
uint64
118443051319296
You can also safely use dec2bin() on int64() and uint64() [at least in the most recent versions], but bin2dec requires special care:
>> S='11010111011100100101100000000000000000000000001';
>> bin2dec(['0b' S 'u64'])
ans =
118443051319297
The ability to use 0b prefix and type suffixes in MATLAB is pretty new.
For older releases, I would reshape the binary to groups of 8, bin2dec(), uint8(), and typecast() to uint64 (but I would double-check that the desired byte order was used; you might need to swapbytes())

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!