We help IT Professionals succeed at work.

# signed unsigned comparison

on
869 Views
Hi,

I've got
long a;
long b;
word c;
a=10;
b=-100;
c=10;
if (a > b + c)
I'm expecting this expression to be true.
Is there any way to force this without changing the expression?
Only compiler settings or some global typedef or something like this
I need this for BCB.
regards
mokule
Comment
Watch Question

## View Solutions Only

Commented:
I'm assuming from your description that word is a 16 bit unsigned integer, in which case you need to cast it to a 32 bit signed integer (you only _need_ to cast it to a 16 bit signed integer, but the compiler is going to extend it up to 32 anyway, so you might as well cast it all the way yourself):

if (a > b + (long)c)

if on the other hand word is a 32-bit unsigned integer, there can be some problems.  If c>0x7fffffff, then the value will not fit into a 32-bit signed integer, and you have to cast everything up to 64-bit ints, but I won't go into that now.

Commented:
I'd like to force desired expression evaluation WITHOUT changing expression

Commented:
Could you explain why do you have this requirement?

Commented:
Are there different solutions depending on why i do need it?
I think no.
But if You really need explanation here You are.

I'm compiling huge program using two compilers for two different environments.
For one compiler
if (a > b + c)
behaves ok and generates shorter code than
if (a > b + (long)c)
So I want to leave this expression as it is.
And I'd like to force BCB to evaluate this expression as I'm expecting
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
> I'd like to force desired expression evaluation WITHOUT changing expression
What are we allowed to change in order to force the evaluation?

Commented:
> digitalseraphim
conditional is what I know myself so far.
But if there are plenty places it won't look good and I must type a lot

>ozo
> What are we allowed to change in order to force the evaluation?
Most preferably some compiler option.

Commented:
What exactly is the problem?

I think the compiler should already be converting c from word to long.

Commented:
ok. i can repeat.
long a;
long b;
word c;
a=10;
b=-100;
c=10;
if (a > b + c)  <-- this evaluates to true in some C compiler for  8051 and to false in C compiler from BDS2006
I'm of course expecting it to be true.
On the other hand, I'd like to avoid
if (a > b + (long)c)
because this is true for both compilers, but gives me longer code for 8051 which is not accaptable.

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Commented:
Forget about 8051 it's all ok for it.
And i don't want to change anything in program for this version.
The problem is with compiler BDS2006

Commented:
Sigh, did you read, and understand my previous post?   Obviously not.

Your path to enlightenment is in there, and nowhere else.

Commented:
I understand enough, don't worry.
You're right.
I've got word defined as unsigned.
for 8051 C compiler long is 4 bytes, unsigned 2 bytes
for BDS2006 both are 4 bytes.
Thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.