Inconsistent behaviour of bitwise shifts

42 views
Skip to first unread message

Michał Pociecha

unread,
Mar 28, 2014, 8:30:13 PM3/28/14
to scala-l...@googlegroups.com
Hello,
I'm curious why bitwise shifts (also >> and >>>) for certain numeric types work in such a strange way.

scala> :paste
// Entering paste mode (ctrl-D to finish)

val int: Int = 1
val long = 2L

val r = int << long
val r2 = int << 2L
val r3 = 1 << long
val r4 = 1 << 2L
val r5 = 1.toInt << long // right, a bit stupid but just to check it out
val r6 = 1.toInt << 2L

// Exiting paste mode, now interpreting.

int: Int = 1
long: Long = 2
r: Int = 4
r2: Int = 4
r3: Int = 4
r4: Long = 4
r5: Int = 4
r6: Int = 4

r4: The compiler changes this 1: Int to Long but why? Is it a bug?

Thanks,
Michal

Michał Pociecha

unread,
Mar 29, 2014, 5:40:30 AM3/29/14
to scala-l...@googlegroups.com
Someone advised me to write on scala-internals so I'll create there similar thread. I consider this one as closed. Sorry for a mess.
Reply all
Reply to author
Forward
0 new messages