A
Alex Vinokur
Hi,
Here is Richard Heathfield's function from http://users.powernet.co.uk/eton/kandr2/krx206.html
#include <stdio.h>
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0
<< n)) << (p + 1 - n));
}
setbits(x,p,n,y) returns x with the n bits that begin at position p
set to the rightmost n bits of y, leaving the other bits unchanged.
It seems that setbits (x, 31, n, y) may produce undefined behavior.
According to the C++ standard:
Behavior of shift operators << and >> is undefined if the right
operand is negative, or
greater than or equal to the length in bits of the promoted left
operand.
So, result ((~0 << (p + 1)) may be undefined.
Regards,
Alex Vinokur
Here is Richard Heathfield's function from http://users.powernet.co.uk/eton/kandr2/krx206.html
#include <stdio.h>
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0
<< n)) << (p + 1 - n));
}
setbits(x,p,n,y) returns x with the n bits that begin at position p
set to the rightmost n bits of y, leaving the other bits unchanged.
It seems that setbits (x, 31, n, y) may produce undefined behavior.
According to the C++ standard:
Behavior of shift operators << and >> is undefined if the right
operand is negative, or
greater than or equal to the length in bits of the promoted left
operand.
So, result ((~0 << (p + 1)) may be undefined.
Regards,
Alex Vinokur