필터 지우기
필터 지우기

Please help me correct or fix my code !!

조회 수: 7 (최근 30일)
Irwin2020
Irwin2020 2020년 7월 21일
댓글: Walter Roberson 2020년 7월 31일
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))

답변 (1개)

Walter Roberson
Walter Roberson 2020년 7월 21일
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
Irwin2020
Irwin2020 2020년 7월 31일
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 2020년 7월 31일
> 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())

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by