Using int64 for changing the data class

3 views (last 30 days)
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.

Accepted Answer

James Tursa
James Tursa on 5 Dec 2018
Edited: James Tursa on 5 Dec 2018
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 Comments
James Best
James Best on 5 Dec 2018
This is a great help, thank you!
Walter Roberson
Walter Roberson on 5 Dec 2018
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.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 5 Dec 2018
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 Comment
James Best
James Best on 6 Dec 2018
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!

Sign in to comment.

Tags

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!