Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Be cautious when iterating bit-shifts

Reply
Thread Tools

Be cautious when iterating bit-shifts

 
 
geo
Guest
Posts: n/a
 
      11-16-2009

#include <stdio.h>
int main()
{ int x=2048;
printf("%d\n",x);
printf("%d\n",((x<<2)>>2) );
printf("%d\n", (x<<2)>>2 );
printf("%d\n",((x<<2)>>2)-x );
printf("%d\n", (x<<2)>>2 -x );
}

Can you predict what five integers
will result from compiling and running
the above program?
Will all compilers give the same results?
 
Reply With Quote
 
 
 
 
Noob
Guest
Posts: n/a
 
      11-16-2009
geo wrote:

> #include <stdio.h>
> int main()
> { int x=2048;
> printf("%d\n",x);
> printf("%d\n",((x<<2)>>2) );
> printf("%d\n", (x<<2)>>2 );
> printf("%d\n",((x<<2)>>2)-x );
> printf("%d\n", (x<<2)>>2 -x );
> }
>
> Can you predict what five integers
> will result from compiling and running
> the above program?
> Will all compilers give the same results?


$ cat foo.c
#include <stdio.h>
int main(void)
{
int x=2048;
printf("%d\n",x);
printf("%d\n",((x<<2)>>2) );
printf("%d\n", (x<<2)>>2 );
printf("%d\n",((x<<2)>>2)-x );
printf("%d\n", (x<<2)>>2 -x );
return 0;
}

$ gcc -std=c89 -pedantic -Wall -Wextra foo.c
foo.c: In function 'main':
foo.c:9: warning: suggest parentheses around + or - inside shift

===> NB gcc's suggestion <===

$ ./a.out
2048
2048
2048
0
2048

INT_MAX must be at least 32767.
2048 << 2 equals 8192, thus x << 2 is always well-defined.
Let y = x << 2

You're considering
1. (y >> 2)
2. y >> 2
3. (y >> 2) - x
4. y >> 2 - x

1. and 2. are obviously equivalent, and equal to x.
3. is equivalent to x - x i.e. 0.
4. is equivalent to y >> (2-x) which is undefined.

Regards.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-16-2009
geo wrote:
> #include <stdio.h>
> int main()
> { int x=2048;
> printf("%d\n",x);
> printf("%d\n",((x<<2)>>2) );
> printf("%d\n", (x<<2)>>2 );
> printf("%d\n",((x<<2)>>2)-x );
> printf("%d\n", (x<<2)>>2 -x );
> }
>
> Can you predict what five integers
> will result from compiling and running
> the above program?


No: Undefined behavior is not predictable. Not on the
basis of the C language, at any rate, although it might be
possible to predict the outcome for a given implementation.

> Will all compilers give the same results?


Unlikely: Undefined behavior is not guaranteed to be
consistent.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      11-19-2009
geo <(E-Mail Removed)> writes:
> printf("%d\n",((x<<2)>>2)-x );


Don't mix bit-wise operations with arithmetic operations
unless you have an over-riding need to.

<< >> & | ^: fine
* / % + - : fine

Cross-pollination leads to mutants.

Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      11-21-2009
Richard Heathfield <(E-Mail Removed)> writes:
> In <(E-Mail Removed)>, Phil Carmody wrote:
>
>> geo <(E-Mail Removed)> writes:
>>> printf("%d\n",((x<<2)>>2)-x );

>>
>> Don't mix bit-wise operations with arithmetic operations
>> unless you have an over-riding need to.
>>
>> << >> & | ^: fine
>> * / % + - : fine

>
> Got me. I put my head on my left shoulder, trying to work out what
> kind of smileys they were.
>
>> Cross-pollination leads to mutants.

>
> And thus to fitter life-forms. Analogies can be dangerous!


Absolutely, "(1u<<n)-1" has great longevity for a reason.

Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
 
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
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
How to be cautious to prevent PRIVACY LEAKS when my class has arrayas instance variable? www Java 4 04-19-2007 08:26 PM
sygate personal firewall over cautious Eli Aran Computer Support 30 02-06-2005 06:52 PM
iterating over arrays with map - problem Mothra Perl 1 05-27-2004 03:37 PM
CheckBoxList -- Iterating Through Items Ron ASP .Net 0 07-15-2003 06:51 PM



Advertisments