AnyCPU
http://anycpu.org/forum/

Is the >> operator portable across different C compilers?
http://anycpu.org/forum/viewtopic.php?f=7&t=111
Page 1 of 1

Author:  barrym95838 [ Mon Jan 27, 2014 1:29 am ]
Post subject:  Is the >> operator portable across different C compilers?

Hello, everyone. As an inexperienced/rusty user of C, I have come across a question for you all.

Does the >> operator shift in zeros or sign-bits in the compilers with which you are familiar. For example:
Code:
/* let's assume that unsigned is 32-bits wide here */
unsigned a = 0x80000000U;
printf("%X", a >> 3);

Should I expect 10000000 or F0000000 to be printed?

I seem to remember reading somewhere that this behavior of >> is compiler-dependent, and I will be using gcc for my 65m32 simulator. I can spend a bit more effort to explicitly deal with the MSBs in my code, should it turn out to be a potential portability issue.

Thanks,

Mike

Author:  Garth [ Mon Jan 27, 2014 5:18 am ]
Post subject:  Re: Is the >> operator portable across different C compilers

I don't use C, but the assemblers I use that have a C-like set of operators shift zeroes in.

Author:  BigEd [ Mon Jan 27, 2014 11:01 am ]
Post subject:  Re: Is the >> operator portable across different C compilers

From what I can find, you can right-shift an unsigned value and you'll get a logical right shift: 80... >> 3 is 10...

If you right-shift a positive signed value you'll get the same behaviour, but if you right-shift a negative signed value what happens is implementation-dependent - so, not portable.

See section 6.5.7 of http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm

Cheers
Ed

Author:  Tor [ Mon Jan 27, 2014 2:00 pm ]
Post subject:  Re: Is the >> operator portable across different C compilers

barrym95838 wrote:
Hello, everyone. As an inexperienced/rusty user of C, I have come across a question for you all.

Does the >> operator shift in zeros or sign-bits in the compilers with which you are familiar.[]

You control that (arithmetic or logical right shift) by using a signed or unsigned type of variable. (signed char) 0x80 >> 1; will give 0xC0 while (unsigned char) 0x80 >> 1; will give 0x40.

However, as BigEd says, the signed number handling is strictly speaking implementation-dependent according to ISO C. But every C compiler I have access to on a range of platforms, admittedly only 16-bit and larger systems, behave consistently as explained above. But to be sure you'll have to test it on the target.

-Tor

Author:  barrym95838 [ Mon Jan 27, 2014 8:53 pm ]
Post subject:  Re: Is the >> operator portable across different C compilers

Thanks, everyone. And now a follow-up:

Let's say that I don't have enough opcode space to include signed and unsigned shifts. Which group type would be more useful to keep in your opinion(s)? I'm going to code up some test cases to see if there's any significant difference in programmer effort between translating one type to the other, but it would also be helpful for me to know what you guys think.

Mike

Author:  Garth [ Mon Jan 27, 2014 9:36 pm ]
Post subject:  Re: Is the >> operator portable across different C compilers

I've always done the signed in software. It seems like unsigned gets used more, and would be more of a pain if it had to be done in software.

Author:  BigEd [ Tue Jan 28, 2014 9:43 pm ]
Post subject:  Re: Is the >> operator portable across different C compilers

Yes, offering unsigned would be the conventional approach, so on the principle of least surprise, that would be a good one.
Edit: oops, I wrote that wrong initially...

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/