Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can you randomly bitwise shift an integer left or right?  (Read 684 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2014-04-12 10:13:30 »

I encountered an IllegalArgumentException for Random class when I'm randomizing my bitwise shift factor in one direction.

1  
this.pixels[tgt + xx] = biomeColor - (biomeColor >> r.nextInt(4));


I thought this is allowed. Or I'm not doing it right?
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2014-04-12 10:17:11 »

Are you really sure, that you called the method with
4
?

This is what the docs say:
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)

The doc says, the implementation looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
 public int nextInt(int n) {
   if (n <= 0)
     throw new IllegalArgumentException("n must be positive");

   if ((n & -n) == n)  // i.e., n is a power of 2
    return (int)((n * (long)next(31)) >> 31);

   int bits, val;
   do {
       bits = next(31);
       val = bits % n;
   } while (bits - val + (n-1) < 0);
   return val;
 }


Therefore the only reason you might get an
IllegalArgumentException
is when you provide a value lower
<= 0
.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online princec

JGO Kernel


Medals: 368
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2014-04-12 10:23:46 »

nextInt() will occasionally give you a zero... which is not a legal argument to << or >>. A bit arbitrary but there you go.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom_mai78101
« Reply #3 - Posted 2014-04-12 10:40:20 »

Therefore the only reason you might get an
IllegalArgumentException
is when you provide a value lower
<= 0
.

nextInt() will occasionally give you a zero... which is not a legal argument to << or >>. A bit arbitrary but there you go.

Cas Smiley

Ah!! That's the problem. So trivial.. Much appreciated.  Tongue
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2014-04-12 10:45:54 »

nextInt() will occasionally give you a zero... which is not a legal argument to << or >>. A bit arbitrary but there you go.

Wut. I didn't know that and it doesn't seem logical to me, but well Cheesy
That's like throwing an IllegalArgumentException when you do
5 + 0
or
42 * 1



See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online princec

JGO Kernel


Medals: 368
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2014-04-12 10:46:33 »

Now I look at it, it does seem strange...
* princec checks...

Cas Smiley

Online princec

JGO Kernel


Medals: 368
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #6 - Posted 2014-04-12 10:47:53 »

It is indeed wrong, 1 << 0 is legal. So I surmise that the code posted above is not actually the code that is throwing the exception.

Cas Smiley

Online princec

JGO Kernel


Medals: 368
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2014-04-12 10:50:45 »

... which brings me to a request for tom_mai... When you get an exception and want to know what it's caused by, please post the full stack trace.

Cas Smiley

Offline tom_mai78101
« Reply #8 - Posted 2014-04-12 11:08:03 »

But... But...

Problem's solved...   Cry
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #9 - Posted 2014-04-12 11:40:52 »

Just to chime in..

 If
 biomecolor 
ever did equal to 0 it would break the code as 0 - 1 due to the shift is in fact -1.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom_mai78101
« Reply #10 - Posted 2014-04-12 12:07:04 »

Yep, hence I hardwired the
biomeColor
to color-specific constants.
Offline Roquen
« Reply #11 - Posted 2014-04-13 06:15:59 »

Shifts are defined for all inputs
Offline tom_mai78101
« Reply #12 - Posted 2014-04-13 09:26:25 »

Glad to know.   Cheesy
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #13 - Posted 2014-04-13 11:31:31 »

Shifts are defined for all inputs

Just not what you might expect* Grin
*right-hand operand is truncated to 5 bits(for an int shift) or 6 bits (for a long shift).

I came a cropper of this behaviour some time ago.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (13 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!