Using int64 for changing the data class

조회 수: 3 (최근 30일)
James Best
James Best 2018년 12월 5일
댓글: James Best 2018년 12월 6일
When using the int64 function on a 32-bit set of data. Does this just add the appropriate number of zeros into the end of the packets of data?
I have read the documentation, and just wanted to confirm that this works the same way as, single to double.
I don't have code to provide as this is a general question, thanks in advance for any help.

채택된 답변

James Tursa
James Tursa 2018년 12월 5일
편집: James Tursa 2018년 12월 5일
int64(x), where x is is a shorter signed or unsigned integer class, will effectively attach the appropriate number of 0 bytes to positive values and sign extend negative values just like you would expect. int64(x), where x is uint64 will either give you the same bit pattern or overflow into the max bit pattern depending on the value of x. Note that this last case is different behavior from what you will get with C/C++ compilers, where the conversion is typically done in a modulo sense and you get the same bit pattern just reinterpreted as signed (although this behavior is not guaranteed).
  댓글 수: 2
James Best
James Best 2018년 12월 5일
This is a great help, thank you!
Walter Roberson
Walter Roberson 2018년 12월 5일
note that single to double does not just add binary 0. single uses a different number of bits for the exponent. 8 bits for the exponent for single and 11 for double . The two have different biases for the exponent too.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 12월 5일
Using int64 to convert a value stored in another type converts the value into the corresponding int64 value (with saturation or rounding if necessary.) In the example below, y has the same values as x but a different type.
If you want to reinterpret the bit pattern, that's a job for typecast. [Depending on how you plan to use the result, you may also need or want to use swapbytes.] In the example below z does not have the same values as x, but is a combination of the bit patterns of x.
Note that z, the result of typecast, is half the length of x. Two int32 values have enough bytes to make one int64.
>> x = int32([4 8 15 16 23 42])
x =
1×6 int32 row vector
4 8 15 16 23 42
>> y = int64(x)
y =
1×6 int64 row vector
4 8 15 16 23 42
>> z = typecast(x, 'int64')
z =
1×3 int64 row vector
34359738372 68719476751 180388626455
You can see the difference if you look at the three variables using format hex.
>> format hex
>> x, y, z
x =
1×6 int32 row vector
00000004 00000008 0000000f 00000010 00000017 0000002a
y =
1×6 int64 row vector
0000000000000004 0000000000000008 000000000000000f 0000000000000010 0000000000000017 000000000000002a
z =
1×3 int64 row vector
0000000800000004 000000100000000f 0000002a00000017
Reset to the default format before you forget.
>> format
There's one other function that I want to mention: cast converts value not bit pattern.
  댓글 수: 1
James Best
James Best 2018년 12월 6일
This was really helpful, makes the data class differences a lot easier to understand.
For this implementation I think I will try to keep the data as an int32.
The dynamic range should be covered by this class.
Thanks for your help, have a nice day!

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

카테고리

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

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by