LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-07-2007, 04:24 PM   #1
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: West Sussex, England
Distribution: Gentoo
Posts: 1,457

Rep: Reputation: 45
Inconsistent bitwise operator behaviour in C


I'm working through K&R to learn C, and I've reached the bit on bitwise operators. I stumbled onto this issue whilst trying to accomplish something else, and have been trying to understand what's going on ever since.

Here's two bits of code. They both output the same numbers by doing left-shift bitwise operations, starting from ~0

The first method, however, shifts to zero from -2147483648; while the second method shifts to -1 from the same number. And I don't understand why.

Code:
int main(void)
{
        int i = ~0;
        int j = 0;

        while (j != 33) {
                i <<= 1;
                printf("%d: %d\n", j++, i);
        }
        return 0;
}
Code:
int main(void)
{
        int i = 0;

        while (i < 33)
                printf("%d: %d\n", i, (~0 << i++));
        return 0;
}
Any clarification would be appreciated!
 
Old 02-07-2007, 04:47 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
There's no inconsistency that I can see. The difference lies in how the loops execute.

In your first example, the variable i is modified (shifted left by one) before the first printf call is made - you never see the initial value of i displayed.

In your second example, you see the initial value (~0) because your loop starts with i=0. Therefore, the first time the printf call is made, ~0 is shifted left 0 times (i.e. left unchanged). Only after that statement executes does i's value increment by one (which later causes "real" shifting to occur).

Last edited by Dark_Helmet; 02-07-2007 at 04:49 PM.
 
Old 02-07-2007, 05:08 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 65
My memory of this isn't great but if I'm right it actually isn't inconsistent.

In example 1 you are taking ~0 (0xFFFFFFF) in our case and shifting it by some constant. The single shift operator in C does wrap indefinitely. In example 2, your starting with ~0 (0xFFFFFFF) and removing 1 bit each time. Eventually you get to 0 and no matter how many bytes you shift 0x00000000 your going to end up with 0.
 
Old 02-07-2007, 05:09 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
Dark_Helment: His blog has a better description of his confusion. When he does the shifting one at a time, it converges to zero, but when he does the shifting all at once, it loops around with a period of 32.

Shifting by a negative number or a number greater than or equal to the data width is undefined in C. GCC just has a convienient definition for the undefined behaviour. Consider less-sane values to shift by for the bit rotation K&R exercise.

Last edited by tuxdev; 02-07-2007 at 05:10 PM.
 
Old 02-08-2007, 03:20 AM   #5
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: West Sussex, England
Distribution: Gentoo
Posts: 1,457

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by tuxdev
Shifting by a negative number or a number greater than or equal to the data width is undefined in C. GCC just has a convienient definition for the undefined behaviour.
Ah! That would explain it. Maybe I should've bought that book on GCC after all..

Cheers!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bitwise >> formula concept... debiant Programming 10 08-31-2006 08:28 PM
Inconsistent behaviour of java.awt in Linux/Windows liangzan Programming 2 09-08-2005 09:34 AM
about bitwise operators? eshwar_ind Programming 17 10-25-2004 02:13 AM
Java [ Bitwise "~" question on process ] k1ll3r_x Programming 2 10-14-2004 11:36 AM
Bitwise operators kamransoomro84 Programming 8 04-22-2004 10:46 PM


All times are GMT -5. The time now is 12:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration