11011110110011011001100110011001000110011001100111010001011

March 30, 2007

判断32位整数二进制中1的个数

Filed under: Programming

晚上回家,收到QQ上河马兄的留言:“计算出一个32位int数中二进制1的个数,不要用逐位比较”(是我们公司的面试题),综合网上的解法如下:

1)内存换速度
char one[256]={0,1,1,2,1,2,……} // 此为 0-255 每个数中 1 的个数
 
int func(int n){
  for(int i=0;n>0;n>>=8)
    i+=one[n&255];
  return i;
}

2)&优化
int func(int n){
  int count=0;
  while(n>0){
    n&=(n-1);
    count++;
  }
  return count;
}

3)最寒的算法,不看注释,还真没看懂
int func(int n)
{
    unsigned int const w = n - ((n >> 1) & 0x55555555);
    unsigned int const x = (w & 0x33333333) + ((w >> 2) & 0x33333333);
    return (((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
}

第一行把n按照2比特分组划分成16组,计算各组中值为1的比特数目;
第二行把n按照4比特分组划分成8组,计算各组中值为1的比特数目;
第三行把n按照8比特分组划分成4组,计算各组中值为1的比特数目,将各组的数累加到高八位上

这里有个总结,很详细:
http://www.everything2.com/index.pl?node_id=1181258

March 27, 2007

Another OpenGl based implementation of M3G

Filed under: Uncategorized

I pick up an article about the M3G implementation based on OpenGL by two Sweden students. it’s an intro. in the paper, and there is no detail descriptions. Just like my implements in KEmulator, we both take use of the comparability between M3G API and OpenGL. The M3G API is designed based on OpenGL, you can find the infos from the m3g specification.

March 26, 2007

Advance Graphics Mode in SWT.GC

Filed under: Programming

In KEmulator 0.7.7, I add rotation control to the Canvas, but it results in bugs on Infos Show!

1. the XOR Pen is disabled.
2. gc.drawRectangle with negative width/height has no efffect.

finally, i find out the reason — the advanced graphics subsystem of GC.

Normally, we can use GC.setAdvanced(boolean advanced) to switched into advanced mode. but the advanced graphics subsystem is invoked automatically when any one of the alpha, antialias, patterns, interpolation, paths, clipping or transformation operations in the receiver is requested. Refer to follows:

setAlpha setAntialias setBackgroundPattern setClipping(Path) setForegroundPattern setInterpolation setTextAntialias setTransform

So, before the Infos painting(drawRectangle/XOR ), use GC.setAdvanced(false) to return normal graphics subsystem.

 (the XOR rect comes back!)

March 24, 2007

java.util.Timer, diffs in J2ME & J2SE

Filed under: Programming

I find out the diffs from the game 3DNeedForDrift, it used Timers to call Canvas.repaint().

It causes an Nullexception in Timer.run()->mainloop()->canvas.repaint(), there is one frame with NULL canvas.

Let’s see the differences of Timer’s implement:

[J2ME/MIDP2.0]

class TimerThread extends Thread {
…….
public void run() {
        try {
            mainLoop();
        } catch (Throwable t) {
            // Someone killed this Thread, behave as if Timer cancelled
            synchronized (queue) {
                newTasksMayBeScheduled = false;
                queue.clear();  // Eliminate obsolete references
            }
        }
    }

private void mainLoop() {
        while (true) {
            try {
                       ……..      
               if (taskFired) { // Task fired; run it, holding no locks
               try {
                       task.run();
                    } catch (Exception e) {
                       // Cancel tasks that cause exceptions
                       task.cancel();
                   }

              }
            } catch (InterruptedException e) {
            }
        }
    }
}

[J2SE/1.4.2]

class TimerThread extends Thread {
…….
public void run() {
        try {
            mainLoop();
        } finally {
            // Somone killed this Thread, behave as if Timer cancelled
            synchronized(queue) {
                newTasksMayBeScheduled = false;
                queue.clear();  // Eliminate obsolete references
            }
        }
    }

private void mainLoop() {
        while (true) {
            try {
                       ……..      
               if (taskFired)  // Task fired; run it, holding no locks
                    task.run();

            } catch (InterruptedException e) {
            }
        }
    }
}

===================

In J2ME, the try…catch… block makes the Timer running normally even if the task throws an exception.

But in J2SE, there is none protection, it’s why the 3DNeedForDrift failed running in KEmulator!

crack rt.jar to run the game:

Canvas rotation in KEmu

Filed under: Programming

In High level UI, be sure of the position(Mouse click) transform.

while the canvas show, the rotation will cause Canvas.sizeChange(int, int)

operation new Tracks

Filed under: Programming

New feature in KEmulator. while the game running, u can get all the "new" operaion infos, exactly the right line in source code.

the follow shots, In Eclipse

March 16, 2007

苍神录2-1通关

Filed under: Uncategorized

终于出了2-1回,一口气玩通关了,累啊,不过这一回内容很丰富啊

通关的装备,13级:

March 15, 2007

File IO in jsr75

Filed under: Uncategorized

File system is supported in KEmulator since version 0.7.4

«« Older Posts