### Looking for an CPU Instruction or a Function Which Can ...

Posted:

**Fri Oct 19, 2012 8:59 am**Hi all,

I come cross a problem on operating the bitboard. I am looking a CPU instruction or a function implemented by asm language or C language. The input of this function is a 64-bit unsigned integer N and a bit number k (0 <= k <= 64). The return value is also a 64-bit unsigned integer R. Suppose the first non-zero bit on left side of bit k is bit k1, and the first non-zero bit on the right side of k is bit k2. Then R is such a 64-bit unsigned integer that all bits from k1 to k2 (including k1 and k2) are 1, but all other bits are 0.

For example:
Another example:
Is there any CPU instruction which can finish such a task, or a good implementation of function which can generate such a 64-bit unsigned integer effciently (do not need to go through each bit one by one from bit k1 to bit k2)?

Thanks a lot!

I come cross a problem on operating the bitboard. I am looking a CPU instruction or a function implemented by asm language or C language. The input of this function is a 64-bit unsigned integer N and a bit number k (0 <= k <= 64). The return value is also a 64-bit unsigned integer R. Suppose the first non-zero bit on left side of bit k is bit k1, and the first non-zero bit on the right side of k is bit k2. Then R is such a 64-bit unsigned integer that all bits from k1 to k2 (including k1 and k2) are 1, but all other bits are 0.

For example:

Code: Select all

```
High bit Low bit
input: N = 0001000000001000100000000000000000000100000000000100000000010000 (64-bit binary integer)
^ ^ ^
k1 k k2
k = 41
output: R = 0000000000000000111111111111111111111100000000000000000000000000 (64-bit binary integer)
```

Code: Select all

```
input: N = 0001000000001000100000000000000000000100000000000100000000010000 (64-bit binary integer)
^ ^ ^
k1 k k2
k = 57
output: R = 0001111111111000000000000000000000000000000000000000000000000000 (64-bit binary integer)
```

Thanks a lot!