Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

switch on a long  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 219
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been programming in Java for a while, and thought I understood the language pretty well, but just recently I tried a piece of code that didn't compile, and I couldn't figure out why. I have here just the section that caused the complaint.

I tried compiling this and got:

It sounds like it's trying to convert from a long to an int. Why would it be doing that? All I'm trying to do is switch off the lowest two bits of value. Does anybody know how to write a switch that will do what I want it to do?
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Simonson wrote:
It sounds like it's trying to convert from a long to an int. Why would it be doing that?


Switching on the long type is not allowed. And the complaint is about casting it to an int type, which can't be done implicitly.

Kevin Simonson wrote:All I'm trying to do is switch off the lowest two bits of value. Does anybody know how to write a switch that will do what I want it to do?


How about casting it to an int type, explicitly?

Henry
 
Fred Kleinschmidt
Bartender
Posts: 544
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use 3L for the constant, which would make it unnecessary to cast to int.
This avoids the problem you would have if value is greater than Integer.MAX_VALUE.
Since (value &3L) is guaranteed to be 3 or less, you can then safely cast the result to an int and switch on that.
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What good would 3L do? The constants after case must be ints (narrower types are implicitly cast to ints) or enum elements or Strings.
 
Liutauras Vilda
Marshal
Posts: 4267
256
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also have to revise your approach in indenting and formatting your code.
There are some decor things, which Java probably isn't a good place to practice at.

I mean
That is as hell confusing, from a first look you couldn't easily figure out if that part even compiles, that would be too difficult question for OCAJP.
 
Campbell Ritchie
Marshal
Posts: 54901
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Simonson wrote:. . . All I'm trying to do is switch off the lowest two bits of value. Does anybody know how to write a switch that will do what I want it to do?
Please avoid coloured text; some people find certain colours very difficult to read.
Switching off bits and the switch statement are two unrelated things and they have nothing to do with each other; maybe it is unfortunate that the same wor‍d is used in both.

Now, as we all know, −1 is this in binary:-
1111_1111_1111_1111_1111_1111_1111_1111
What you want is to lose the rightmost two bits. So you can consider a bitwise AND, but what with? Remove the rightmost two bits from that number and you get
1111_1111_1111_1111_1111_1111_1111_1100
which is −4. You have removed the 1₀ (=1) and the 1₁ (=2) from it, so it goes down by 3.

The easiest way to obliterate those two rightmost bits is
i & −4
If you wish to use 3, you would have to use the one's complement operator:-
i & ~3
And let's remember: ~3 equals −4.
It shou‍ld make no difference whether those numbers are ints or longs, but if would be a good idea to add the L if using longs.
 
Fred Kleinschmidt
Bartender
Posts: 544
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What good would 3L do? The constants after case must be ints (narrower types are implicitly cast to ints) or enum elements or Strings.

I was thinking:

But I realize that 3L is not necessary there; 3 would work as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!