View unanswered posts | View active topics It is currently Thu Mar 28, 2024 9:06 pm



Reply to topic  [ 7 posts ] 
 Is the >> operator portable across different C compilers? 
Author Message

Joined: Tue Dec 31, 2013 2:01 am
Posts: 116
Location: Sacramento, CA, United States
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


Mon Jan 27, 2014 1:29 am
Profile

Joined: Tue Dec 11, 2012 8:03 am
Posts: 285
Location: California
I don't use C, but the assemblers I use that have a C-like set of operators shift zeroes in.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Mon Jan 27, 2014 5:18 am
Profile WWW

Joined: Wed Jan 09, 2013 6:54 pm
Posts: 1780
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


Mon Jan 27, 2014 11:01 am
Profile

Joined: Tue Jan 15, 2013 10:11 am
Posts: 114
Location: Norway/Japan
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


Mon Jan 27, 2014 2:00 pm
Profile

Joined: Tue Dec 31, 2013 2:01 am
Posts: 116
Location: Sacramento, CA, United States
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


Mon Jan 27, 2014 8:53 pm
Profile

Joined: Tue Dec 11, 2012 8:03 am
Posts: 285
Location: California
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.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Mon Jan 27, 2014 9:36 pm
Profile WWW

Joined: Wed Jan 09, 2013 6:54 pm
Posts: 1780
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...


Tue Jan 28, 2014 9:43 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software