Please help me correct or fix my code !!
이전 댓글 표시
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
2020년 7월 21일
0 개 추천
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
2020년 7월 21일
Walter Roberson
2020년 7월 21일
extractBefore leaves the values as character, which does not work for bit shifts.
Irwin2020
2020년 7월 27일
Walter Roberson
2020년 7월 27일
편집: Walter Roberson
2020년 7월 27일
once you have converted the hex to unsigned integer, then use bitshift and similar. The reason not to just divide by 2^shift is uint8 operations are defined as uint8(operation(double(operands)) and converting double to uint8 is by rounding not truncation. For example uint8(3)/uint8(2) is uint8(double(uint8(3))/double(uint8(2))) which is uint8(1.5) which is uint8(2), whereas bitshift would produce uint8(1)
I am not sure of the context for the subtraction you are discussing? Are you talking about how to do two's complement arithmetic? Or are you asking about signed shift?
Irwin2020
2020년 7월 31일
Walter Roberson
2020년 7월 31일
When you indicate that F0 should correspond to -15 then that implies that FF should correspond to -0 with 00 corresponding to +0. Systems that have both a negative and positive zero are either One's Complement or else Separate Sign. You appear to be using One's Complement.
Is this correct? If it is then one of the implications is that FF + 01 would be 01 since FF would correspond to 0. In the One's Complement system you need to make an adjustment when the calculation crosses 0 in either direction, which is not something that has to be done with Two's Complement.
Does your system require division? The major practical difference between systems that have signed zero or not is that in systems with signed zero then division by 0 gives sign(numerator)*sign(the zero) *infinity so for example -5/-0 gives +infinity. Whereas when zero is unsigned the result is sign(numerator)*infinity
Irwin2020
2020년 7월 31일
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())
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!