Velocity Reviews > Bitwise operators.

# Bitwise operators.

Joachim Schmitz
Guest
Posts: n/a

 01-16-2008
Philip Potter wrote:
> CBFalconer wrote:
>> Robert Latest wrote:
>>> 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

pete
Guest
Posts: n/a

 01-16-2008
Joachim Schmitz wrote:
>
> Philip Potter wrote:
> > CBFalconer wrote:

> >> 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"

/* 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.

--
pete

Joachim Schmitz
Guest
Posts: n/a

 01-16-2008
pete wrote:
> Joachim Schmitz wrote:
>>
>> Philip Potter wrote:
>>> CBFalconer wrote:

>
>>>> 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"

>
> /* 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

pete
Guest
Posts: n/a

 01-16-2008
Joachim Schmitz wrote:
>
> pete wrote:
> > Joachim Schmitz wrote:
> >>
> >> Philip Potter wrote:
> >>> CBFalconer wrote:

> >
> >>>> However, barring arithmetic
> >>>> overflow, ((x << 2) == (x * 4)) is guaranteed for unsigned x.

> >> Chuck wrote "for unsigned x"

> > In that case, arithmetic overflow, is irrelevant.

> Yes?

Yes.

--
pete

pete
Guest
Posts: n/a

 01-16-2008
pete wrote:
>
> Joachim Schmitz wrote:
> >
> > pete wrote:
> > > Joachim Schmitz wrote:
> > >>
> > >> Philip Potter wrote:
> > >>> CBFalconer wrote:
> > >
> > >>>> However, barring arithmetic
> > >>>> overflow, ((x << 2) == (x * 4)) is guaranteed for unsigned x.

>
> > >> Chuck wrote "for unsigned x"

>
> > > In that case, arithmetic overflow, is irrelevant.

>
> > Yes?

>
> Yes.

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."

--
pete

Joachim Schmitz
Guest
Posts: n/a

 01-16-2008
pete wrote:
> Joachim Schmitz wrote:
>>
>> pete wrote:
>>> Joachim Schmitz wrote:
>>>>
>>>> Philip Potter wrote:
>>>>> CBFalconer wrote:
>>>
>>>>>> However, barring arithmetic
>>>>>> overflow, ((x << 2) == (x * 4)) is guaranteed for unsigned x.

>
>>>> Chuck wrote "for unsigned x"

>
>>> In that case, arithmetic overflow, is irrelevant.

>
>> Yes?

>
> Yes.

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

Bye, Jojo

Philip Potter
Guest
Posts: n/a

 01-16-2008
Joachim Schmitz wrote:
> pete wrote:
>> Joachim Schmitz wrote:
>>> pete wrote:
>>>> Joachim Schmitz wrote:
>>>>> Philip Potter wrote:
>>>>>> CBFalconer wrote:
>>>>>>> However, barring arithmetic
>>>>>>> overflow, ((x << 2) == (x * 4)) is guaranteed for unsigned x.
>>>>> Chuck wrote "for unsigned x"
>>>> In that case, arithmetic overflow, is irrelevant.
>>> Yes?

>> Yes.

> 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