|
operation boot camp
need help with bitshift operation Please help me divide a number with 24 using bitshift operationi know to multiply a number with 24x = (x<<4) + (x<<3)but how to divide it with 24???thanks a lot In the first place bitshift is binary bit manipulation and not maths so it cannot really be done in the way you have envisaged and in the second place, I could wrack my head all day and not come up with a single reason as why /what circumstances it would be necessary to do such a thing.
Above all binary 255 eg: is just a number, which you can convert (if necessary) to decimal and convert it back to binary if that's what you need to do - the answer to your question is
I/=24;
I think this should work...
(I dunno if its how your sposed to do it though, I made it up myself ^_^)btw, its worth noting, both the div and mul will only work for positive (unsigned) integers.public static int shiftMul24(final int x) { return (x<<4) + (x<<3);
} public static int shiftDiv24(final int x) { int newX=x;
Int sum=0; do { sum+=newX>>>5;
NewX = x-shiftMul24(sum);
} while(newX>=32);
If(newX>=24) sum++;
Return sum; } Slight modification to make it more efficient,the line :-while(newX>=32);should read :-while(newX>=48); Wow thanxx a lot Abuse!!
You're GREAT!! :)umm anyway can u explain the number in therenewX>>>5, while(newX>=32), if(newX>=24) sum++;i want to use it for other numberand what's the final word in final int xfor???and thanxx for the explanation The-Sue i have my own reason to do this :)thank you very much!!
operation boot camp Ok, heres the commented version ;)I've also added a DEBUG flag, which gives a good explanation of what is happening during the division process.public class Test { static final operation boot camp DEBUG = true;
//the debug lines explain best exactly how the method works public static int shiftMul24(final int x)// for 'correctness' the parameter is marked final(can't be modified) { return (x<<4) + (x<<3);
//effectively x*16 + x*8 } public static int shiftDiv24(final int x)// for 'correctness' the parameter is marked final(can't be modified) { int newX=x;//the remainder int sum=0;//the accumulator do { sum+=newX>>>5;
//^^ effectively newX/32 (32 is the next power of 2 above 24) //the result is then accumulated in 'sum' newX = x-shiftMul24(sum);
//^^ using the original parameter(x) and the new sum(sum), the remainder(newX) is recalculated for the next iteration if(DEBUG)System.out.println(x+"/24 is " + sum + " remainder " + newX);
} while(newX>=48);//if newX can be divided 2 or more times by 24, then more iterations are required if(newX>=24) sum++;//finally, if the final remainder is >=24, the final sum needs incrementing by 1 if(DEBUG)System.out.println(x+"/24 is " + sum + " remainder " + (newX-24));
Return sum; } } operation boot camp happy new year comments > and thanxx for the explanation The-Sue i have my own reason to do this :)I'd love to know what those reasons are 2 :DI'm guessing its a school exercise of some kind?
Doh, found a bug in the debug code ;)The 2nd debug line should read :-if(DEBUG)System.out.println(x+"/24 is " + sum + " remainder " + (newX>=24?newX-24:newX)); caucus Try a google searh for 'iterative division using shifts'There is 1 article that seems to hawaii athletics the algorithm - but its in asm :p operation boot camp Thanxx Abuse, i'll try to understand the code right now :)it's not for school exerciseonly want to use bitshift for my 24x24 tile backgroundthis one i use to get the shift operatorpublic static int[] getShiftOperation(int num) { if (num % 2 != beverly hillbillies throw new RuntimeException("num must be the power of two, 2,4,6,8,...");
Int[] base = new int[1];
Int i = 1; while (Math.pow(2,i) <
Num) i++; if (Math.pow(2,i) >
Num) { int mod = num;
Base[0] = --i; do { mod = mod-(int)Math.pow(2,base[base.length-1]);
I = shiftOp(mod);
If (i != 0) { base = (int[]) Utility.expand(base,1);
Base[base.length-1] = i;
} } while (i != 0);
} else base[0] = i;
Return base; } private static int shiftOp(int num) { if (num == 0) return 0;
Int i = 1; while (Math.pow(2,i) <
Num) i++; if (Math.pow(2,i) >
Num) i--; return i;
}and to multiply it with numpublic static int shiftMultiply(int num, int[] shift) { int result = 0;
For (int i=0;i <
Shift.length;i++) result += num< Return result; }thanxx>
:oum, you are doing all that to avoid using the div operator?!?!may sara jane moore why?
Division isn't slow :-/ 2008 nfl draft order Umm?? really??but i see bitshift is the one that recommended to use in every java optimization articleand i use this cos i got a low rate fps only for drawing my tile backgroundgot about 40 fps on my P-IV (drops about 20 fps)i don't know which one that make it dropsso i try all technique in optimization article :)still can't get the shiftDiv work on every tom brady :(for 24 it works perfect :)thanxx anyway Have you profiled your code?I am 110% sure using bitshifts will have no impact on performance *at all*.
(infact, attempting to do division with bitshifts only is likely to be many many times slower)Unless you are doing something very peculiar, the bottleneck in a game is always going to be either the image drawing, or collision detection.
operation boot camp I don't know how operation boot camp use java profiler :(it makes me confuse operation boot 1972 dolphins see all the magic number there :pbut i think my code is really simpleonly iterate about 20x15 = operation boot camp (640/32 x 480/32) to draw tiles on every operation boot camp loopso it's not neccesary to use any profiler for nowumm if using bitshifts will have no impact on performance *at all*why all optimization article always said that????!?!?!is all that optimization thing is ???if that true i will stick back to the old division :)anyway a simple question:is draw 300 tiles as fast as draw one big image (640x480) in every game loop???is iterate on 300 tiles is slow??
If that so, i'll make my tile bigger :)thanxx a lot Abuse Drawing 300 tiles will be marginally slower than drawing a single large image, however the difference isn't worth worrying about.300 drawImages shouldn't be a problem if you are using managed images.on my PC I can draw about 6000 32x32 managed images at 30fps.
myspace new years comments
Popular topics today: buynowbe
|