Home > Java, Mini-tips, Programación > Multiplicar y dividir en potencias de 2 más rápido

Multiplicar y dividir en potencias de 2 más rápido

September 12, 2011 Leave a comment Go to comments

Multiplicar:
x << n == x * 2^n


int x = 3;
System.out.println(x << 1); //imprime 6
System.out.println(x << 2); //imprime 12
System.out.println(x << 3); //imprime 24

Dividir:
x >> n == x / 2^n


int x = 13;
System.out.println(x >> 1); //imprime 6
System.out.println(x >> 2); //imprime 3
System.out.println(x >> 3); //imprime 1
Categories: Java, Mini-tips, Programación
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: