Java Bit Shift Left Returns Wrong Value

I'm having problems with left bit shifts in Java returning incorrect values...

Take 108 << 60 for instance. The answer should be*:

124515522497539473408

Java is returning this value

-4611686018427387904

for this statement:

System.out.println(108L << 60L);

Why??? Both values are forced longs... so I see no reason why any bit values should be truncated. What am I missing here?

*Citation: Wolfram Alpha

Answers


You are shifting beyond the length of a long (64 bits). 108 occupies seven bits, so 108L << 60L requires 67 bits to represent it correctly. Actually, since it's a signed type, you'd need 68 bits to avoid having it interpreted as a negative number.


Need Your Help

Column count doesn't match value count at row 1 with 2 arrays of same length

php mysql save

I'm trying to save to a db, and I need to implement a system in which users can specify in which fields of a given table the data has to be saved.

dynamically added form element ids not found

javascript jquery ajax forms

I have a form in which a user can click a button and add additional fields as needed. Later on, user clicks submit, i do some error checking via ajax. if the additional fields are blank, i want to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.