Tuesday, 15 April 2014

java - Advantage of >> operator over / operator -



java - Advantage of >> operator over / operator -

what advantage of using >> operator on / operator? extensively used in code maintaining. eg, int width = previouswidth >> 2;

when want shift value number of bits, it's considerably simpler understand. example:

byte[] bits = new byte[4]; bits[0] = (byte) (value >> 24); bits[1] = (byte) (value >> 16); bits[2] = (byte) (value >> 8); bits[3] = (byte) (value >> 0);

that's shifting different numbers of bits. really want express in terms of partition instead?

now of course of study when want division, should utilize partition operator sake of readability. people may utilize bitshifting sake of performance, ever, readability more of import micro-optimization code. in case, if what's actually desired width previouswidth divided 4, code should *absolutely reflect that:

int width = previouswidth / 4;

i'd utilize bitshifting after proving performance difference significant.

java

No comments:

Post a Comment