java - I need help on Bit Twiddling -


i love see people writing bit twiddling code can't understand @ all. gone through hacker's delight , http://graphics.stanford.edu/~seander/bithacks.html, failed understand anything.

for example:

how come 1 | 2 returns 3 or how come a ^=b; b ^= a; ^=b; swap values etc...

one method:

private t[] ensurecapacity(int mincapacity) {    if (tmp.length < mincapacity) {    // compute smallest power of 2 > mincapacity    newsize |= newsize >> 1;    int newsize = mincapacity;    newsize |= newsize >> 2;    newsize |= newsize >> 4;    newsize |= newsize >> 8;    newsize |= newsize >> 16;    newsize++;      if (newsize < 0) // not bloody likely!       newsize = mincapacity;     else       newsize = math.min(newsize, a.length >>> 1);        @suppresswarnings({"unchecked", "unnecessarylocalvariable"})       t[] newarray = (t[]) new object[newsize];       tmp = newarray;    }    return tmp;  } 

what below likes doing:

int newsize = mincapacity; newsize |= newsize >> 1; newsize |= newsize >> 2; newsize |= newsize >> 4; newsize |= newsize >> 8; newsize |= newsize >> 16; newsize++; 

or

newsize = math.min(newsize, a.length >>> 1); 

better use >> or >>> operator mean after joshua bloch fixed broken binary search understand it's safe use >>> instead >>. please , if there tutorial above mentioned source appreciate much.

what easiest way calculate output of bits example 1 | 2 = 3?

i mean don't know how bit form looks except use calculator or something.. there easiest way calculate these things without in mind?

what easiest way calculate output of bits example 1 | 2 = 3.

write out numbers binary. how numbers represented.

  00000001 | 00000010 = 00000011 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -