435,254 Members | 2,477 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,254 IT Pros & Developers. It's quick & easy.

shift question

 P: n/a Hi there! I have a very silly shift operation question: My test code does something like this: int j=1; int y=0; int i=0; int bs=0; for(i=0;i<16;i++){ bs=1<
3 Replies

 P: n/a horvatj wrote: My test code does something like this: int j=1; int y=0; int i=0; int bs=0; for(i=0;i<16;i++){ bs=1< ... int x = 512; int y; y = log(x) / log(2); Another method would be to look for the first bit that is set to 1 with a simple loop (assuming 16 bit integers, like in your example) : int x = 512; int y; for(y=15; y>=0; y--) if(x & (1<>= 1; y --; } This can be further optimized by using binary search methods. If you are really interested, you can find more recipes in the book "Hacker's Delight", chapter 5-3. -- :wq ^X^Cy^K^X^C^C^C^C Apr 16 '06 #2

 P: n/a "Ico" wrote in message news:44**********************@dreader30.news.xs4al l.nl... horvatj wrote: My test code does something like this: int j=1; int y=0; int i=0; int bs=0; for(i=0;i<16;i++){ bs=1< ... int x = 512; int y; y = log(x) / log(2); Another method would be to look for the first bit that is set to 1 with a simple loop (assuming 16 bit integers, like in your example) : int x = 512; int y; for(y=15; y>=0; y--) if(x & (1<>= 1; y --; } Apr 16 '06 #3

 P: n/a stathis gotsis wrote: "Ico" wrote in message Your question is not very clear, but I assume that what you are looking for is the base-2 logarithm of a number. If performance is not a big issue, you can use the log() function from your local math library : #include ... int x = 512; int y; y = log(x) / log(2); Another method would be to look for the first bit that is set to 1 with a simple loop (assuming 16 bit integers, like in your example) : int x = 512; int y; for(y=15; y>=0; y--) if(x & (1<

This discussion thread is closed

Replies have been disabled for this discussion. 