Using int64 for changing the data class

3 次查看(过去 30 天)
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 个评论
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