How to convert long to int in Java vs. swift?

I tried converting this code to Java in swift 2.0

long checksumValue = 4253726258L; int liCrc32 = (int)checksumValue; System.out.println("liCrc32" + liCrc32) 

LiCrc32 is -41241038

My quick version

 let int64: Int64 = 4253726258 let a:Int = Int(int64) 

a returns me 4253726258

What I donโ€™t understand, why is this negative value in Java?

+6
source share
3 answers

You are using the wrong type: Swift Int does not necessarily correspond to Java Int because its size is platform dependent. Judging by your result, your platform uses 64-bit Int s. Here's how to confirm it:

 println(sizeof(Int)) // Should print 8 on your platform 

Int32 corresponds to Java Int . Sorry, this change

 let int64: Int64 = 4253726258 let a:Int32 = Int32(int64) 

may cause problems due to an error in the implementation of Swift.

If you need to sign an extended 32-bit value, you can do it as follows:

 let int64: Int64 = 4253726258 let a:Int = Int((int64 << 32) >> 32) println(a) // prints -41241038 

Demo version

+3
source

From the Swift documentation

Int

In most cases, you do not need to select a specific size of integers to use in your code. Swift provides an additional integer type, Int, which is the same size as the current size of the native platform language:

  • On a 32-bit platform, Int is the same size as Int32 .

  • On a 64-bit platform, Int is the same size as Int64 .

If you don't need to work with a specific size of integers, always use Int for integer values โ€‹โ€‹in your code. This helps to harmonize code and compatibility. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647 and is large enough for many integer ranges.

Java Int , on the other hand, is always 4 bytes.

Integrated types byte , short , Int and long , the values โ€‹โ€‹of which are 8-bit, 16-bit, 32-bit and 64-bit signed integers with two additions, respectively, [...]

Listing from long to Int narrows the primitive conversion

Narrowing a primitive conversion may lose information about the total value of a numerical value, and may also lose accuracy and range.

+4
source

int: By default, the int data type is a 32-bit signed integer with two values, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent a 32-bit unsigned integer that has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data, enter as an unsigned integer. See the โ€œNumber Classesโ€ section for more information. Static methods such as compareUnsigned, divideUnsigned, etc. have been added to the Integer class to support operation arithmetic for unsigned integers.

long: The long data type is a 64-bit integer from two additions. The recorded long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use a long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values โ€‹โ€‹than those provided by int. The Long class also contains methods such as compareUnsigned, divideUnsigned, etc. to support arithmetic operations for unsigned long.

The value that int in Java <J8 can be maximal:

 2.147.483.647 

which is smaller than your int: 4.253.726.258

To do this, it cannot fit into int , you need to either stick to long , or upgrade Java to version 8 and make unsigned int.

What happens with the conversion is that after 2.147.483.647 the counter starts with the minimum number (-2.147.483.647) and finally gives you -41241038 , which is -2.147.483.647+ (4.253.726.258 - 2.147.483.647)

+2
source

All Articles