One optimal algorithm

xiaoxiao2021-03-06  94

Problem: For a positive integer N, the maximum integer of an index that is not more than N and is 2.

INT floor (int N) {

n = n | n >> 1;

N = N | n >> 2;

n = n | n >> 4;

n = n | n >> 8;

n = n | n >> 16;

Return n- (n >> 1);

}

It's really the most efficient program I found, as long as 12 instructions.

In addition, it is generally like this problem.

m = 1;

While (m <= n) m << 1;

RETURN M >> 1;

It looks more concise but the efficiency is low.

转载请注明原文地址:https://www.9cbs.com/read-104246.html

New Post(0)