Что-то я не соображу.. как можно сделать проще в голову не приходит, но если считать функцию setFirstNBits за операцию, тогда превышение операций..
C++ |
1
2
3
4
5
6
7
8
9
| /*
* fromNtoMBits - returns a number composed from bits n to m
* Example: fromNtoMBits(4, 3, 5) = 1, fromNtoMBits(12, 3, 5) = 3
* Legal ops: >> & - setFirstNBits
* Max ops: 4
*/
int fromNtoMBits( int x, int n, int m ) {
return (x >> (n - 1)) & setFirstNBits( m - n );
} |
|
Добавлено через 8 минут
C++ |
1
2
3
4
5
6
7
8
9
10
| /*
* thirdBits - return word with every third bit (starting from the least
significant bit, LSB) set to 1 and the rest set to 0
* thirdBits() & 1 == 1, thirdBits() & 8 == 8, etc.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
*/
int thirdBits() {
return 0x49249249;
} |
|
Так конечно на вряд ли можно, но для 32-бинтых систем работает))