Re: bit position
On Jun 3, 7:00 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
I believe this is possible:
unsigned or_bit(unsigned val, unsigned bit) {
assert(bit < (unsigned)log2l (UINT_MAX);
....
Actually, can this problem be solved fully portably? (I guess there
isn't a guarantee that LDBL_MAX UINT_MAX)
On Jun 3, 7:00 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
Chris Thomasson wrote:
>
>
>
>
>
>
>
>
>
ITYM `1u << bit'. Also, what's the point in asserting
`bit >= 0'?
>
Finally, there's the theoretical possibility of padding
bits: It's conceivable (albeit unlikely) that the other
asserted condition is too lax.
>
<vipps...@gmail .comwrote in message
news:cf77c2ed-baf8-4961-8fbd-ebdd20445c2d@79 g2000hsk.google groups.com...
news:cf77c2ed-baf8-4961-8fbd-ebdd20445c2d@79 g2000hsk.google groups.com...
On Jun 3, 4:34 pm, Chris Dollin <chris.dol...@h p.comwrote:
>Chris Thomasson wrote:
"Chris Dollin" <chris.dol...@h p.comwrote in message
>"Validation" ?
>Chris Thomasson wrote:
"Chris Dollin" <chris.dol...@h p.comwrote in message
>"Validation" ?
Perhaps he means something like the assertion in the following >
>function:
>function:
int or_bit(int val, unsigned bit) {
assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
return val | (1 << bit);
}
assert(bit >= 0 && bit < (sizeof(int) * CHAR_BIT));
return val | (1 << bit);
}
>Oh, I see.
>I probably wouldn't do /that/.
Cannot it invoke undefined behavior?
I was under the impression you cannot freely operate on all bits of a
signed integer.
Cannot it invoke undefined behavior?
I was under the impression you cannot freely operate on all bits of a
signed integer.
Well...
#include <limits.h>
unsigned or_bit(unsigned val, unsigned bit) {
assert(bit >= 0 && bit < (sizeof(unsigne d) * CHAR_BIT));
return val | (1 << bit);
}
assert(bit >= 0 && bit < (sizeof(unsigne d) * CHAR_BIT));
return val | (1 << bit);
}
ITYM `1u << bit'. Also, what's the point in asserting
`bit >= 0'?
>
Finally, there's the theoretical possibility of padding
bits: It's conceivable (albeit unlikely) that the other
asserted condition is too lax.
unsigned or_bit(unsigned val, unsigned bit) {
assert(bit < (unsigned)log2l (UINT_MAX);
....
Actually, can this problem be solved fully portably? (I guess there
isn't a guarantee that LDBL_MAX UINT_MAX)
Comment