Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write a hexadecimal integer literal equal to Int.MIN_VALUE in Kotlin

how does one write a hexadecimal integer literal that is equal to Int.MIN_VALUE (which is -2147483648 in decimal) in Kotlin?

AFAIK, an Int is 4 bytes...and sometimes it seems like 2's complement is used to represent integers...but I'm not sure. I've tried the following hex literals to help myself understand the system:

  • 0xFFFFFFFF but this is a Long, not an Int
  • 0xFFFFFFFF.toInt() which is -1
  • -0xFFFFFFFF.toInt() which is 1
  • 0x7FFFFFFF which is 2147483647 which is Int.MAX_VALUE
  • -0x7FFFFFFF which is -2147483647 which is Int.MIN_VALUE+1
  • 0xFFFFFFF which is 268435455 in decimal
  • 0x0FFFFFFF which is also 268435455 in decimal

But I can't figure out what hexadecimal integer literal can be used to represent Int.MIN_VALUE.

I hope the answer doesn't make me feel stupid...

like image 663
Eric Avatar asked Jul 04 '16 01:07

Eric


People also ask

What are hexadecimal integer literals?

A hexadecimal integer literal begins with the 0 digit followed by either an x or X, followed by any combination of the digits 0 through 9 and the letters a through f or A through F. The letters A (or a) through F (or f) represent the values 10 through 15, respectively.

How do I get int with Kotlin?

To convert a string to integer in Kotlin, use String. toInt() or Integer. parseInt() method. If the string can be converted to a valid integer, either of the methods returns int value.

What is int Kotlin?

Int is a Kotlin Class derived from Number . See doc. [Int] Represents a 32-bit signed integer. On the JVM, non-nullable values of this type are represented as values of the primitive type int. Integer is a Java Class.


1 Answers

Int represents a 32-bit signed integer. 32 bits means 8 hex digits:

___7 F F F F F F F

0111 1111 1111 1111 1111 1111 1111 1111

As you can see the left-most bit is 0 thus this is a positive integral in a 32 bit representation. By 2's complement definition and example the minimal 32-bit negative value will have 1 at left-most bit followed by 0:

1000 0000 0000 0000 0000 0000 0000 0000

___8 0 0 0 0 0 0 0

that is 0x80000000.

In Kotlin you need to prepend the - sign to denote negative Int which is not true in Java. Consider following example

println(0x7FFFFFFF) // -> prints 2147483647 (Integer.MAX_VALUE)
println(-0x80000000) // -> prints -2147483648 (Integer.MIN_VALUE)
println(0x80000000) // -> prints 2147483648 (does not fit into Int)

It's not the same as in Java:

System.out.println(0x7FFFFFFF); // -> prints 2147483647 (Integer.MAX_VALUE)
System.out.println(-0x80000000); // -> prints -2147483648 (Integer.MIN_VALUE)
System.out.println(0x80000000); // -> prints -2147483648 (Integer.MIN_VALUE)

This is in line with Kotlin spec although the overflow behavior of hexadecimal literals is yet to be defined.

Further reading:

  • Why is Java able to store 0xff000000 as an int?
like image 196
miensol Avatar answered Oct 21 '22 23:10

miensol