Bitwise operators.

J

Joachim Schmitz

Philip said:
CBFalconer said:
Robert said:
dspfun wrote:

These operators yield values that depend on the internal
representations of [unsigned] integers
Question is: Is --for instance-- (x << 2) == (x * 2) guaranteed?
I thought so.

It is guaranteed to be false.

Not so. x == -1 produces UB in the LH operand, so it could be true.
Slightly more plausibly, if x == 0 then the expression is guaranteed
to be true.
However, barring arithmetic
overflow, ((x << 2) == (x * 4)) is guaranteed for unsigned x.
Again, not so. For x == -1, no overflow occurs but UB does occur so no
guarantees exist.
Chuck wrote "for unsigned x"

Bye, Jojo
 
P

pete

Joachim said:
Chuck wrote "for unsigned x"

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
unsigned x = 0u - 1;

if ((x << 2) == (x * 4)) {
puts("In that case, arithmetic overflow, is irrelevant.");
}
return 0;
}

/* END new.c */

N869
6.2.5 Types
[#9]
A computation involving unsigned operands
can never overflow, because a result that cannot be
represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the
largest value that can be represented by the resulting type.

6.5.5 Multiplicative operators
[#4] The result of the binary * operator is the product of
the operands.

6.5.7 Bitwise shift operators
[#4] The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an
unsigned type, the value of the result is E1×2E2, reduced
modulo one more than the maximum value representable in the
result type.
 
J

Joachim Schmitz

pete said:
Joachim said:
Chuck wrote "for unsigned x"

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
unsigned x = 0u - 1;

if ((x << 2) == (x * 4)) {
puts("In that case, arithmetic overflow, is irrelevant.");
}
return 0;
}

/* END new.c */

N869
6.2.5 Types
[#9]
A computation involving unsigned operands
can never overflow, because a result that cannot be
represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the
largest value that can be represented by the resulting type.

6.5.5 Multiplicative operators
[#4] The result of the binary * operator is the product of
the operands.

6.5.7 Bitwise shift operators
[#4] The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an
unsigned type, the value of the result is E1×2E2, reduced
modulo one more than the maximum value representable in the
result type.
Yes? So where's the UB Philip claimed?

Bye, Jojo
 
P

pete

pete said:

CBFalconer says:
"barring arithmetic overflow,
((x << 2) == (x * 4)) is guaranteed for unsigned x."

pete says:
"((x << 2) == (x * 4)) is guaranteed for unsigned x."
 
P

Philip Potter

Joachim said:
You didn't quote nor answer my whole question:
Where is the UB Philip claimed

There isn't any, you are quite right, and pete was not claiming there
was either. He was pointing out that arithmetic overflow has nothing to
do with it, and I'd add that it never does for unsigned types. But a
minor nit compared to the UB I thought was there.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top