Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bitwise operators.

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
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 E12E2, reduced
modulo one more than the maximum value representable in the
result type.

--
pete
 
Reply With Quote
 
 
 
 
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 E12E2, reduced
> modulo one more than the maximum value representable in the
> result type.

Yes? So where's the UB Philip claimed?

Bye, Jojo


 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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


 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BitWise Operations =?Utf-8?B?Sm9u?= ASP .Net 3 01-24-2006 09:19 AM
bitwise comparator Random ASP .Net 2 06-08-2005 05:28 PM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-21-2003 03:55 AM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 1 10-21-2003 03:46 AM
Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-20-2003 09:26 PM



Advertisments