Please help me correct or fix my code !!
7 ビュー (過去 30 日間)
古いコメントを表示
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))
0 件のコメント
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!