Board index » cppbuilder » u_32 result truncated to u_16

u_32 result truncated to u_16


2006-01-07 04:50:47 PM
cppbuilder5
Hi,
I'm using version 4.52 with target 80186. when I do:
a = b * 100;
when a is 32 bit, but b is u_16 then result is truncated to 16 bit,
except when I make the literal number long:
a = b * 100L;
Is then my conclusion right that if we need a 32 bit result that at
least 1 of the variables / numbers where calculating with has to be also
a 32 bit ?
Are there other exceptions where a result can truncate for example in
more complex calculations ?
thanks, Wilfried
 
 

Re:u_32 result truncated to u_16

You are correct. How it works is specified by the language standard.
Assuming that 'a' is unsigned long, 32 bits, and 'b' is unsigned int, 16
bits
a = b * 100;
The unsigned int literal, '100', will be promoted to an unsigned int to
match the type of 'b'. The multiplication will be made and the result is an
unsigned int. The assignment is across types so the result is converted to
the unsigned long type of 'a'.
Each operation or sub operation is done at a level of the highest type of
the two operators. To guarantee that you will have no truncation, arrange
that at least one of the two operators in each sub operation is of the level
you wish for the final result by the types of the items in the expression or
by using a cast. I am using the term sub operation to refer to an operation
which is done in an expression where that operation is not the entire
expression such as 'a + b * c' where the multiplication and addition are
each sub operations.
. Ed
Quote
Wilfried Mestdagh wrote in message
news: XXXX@XXXXX.COM ...
Hi,

I'm using version 4.52 with target 80186. when I do:

a = b * 100;

when a is 32 bit, but b is u_16 then result is truncated to 16 bit, except
when I make the literal number long:

a = b * 100L;

Is then my conclusion right that if we need a 32 bit result that at least
1 of the variables / numbers where calculating with has to be also a 32
bit ?

Are there other exceptions where a result can truncate for example in more
complex calculations ?
 

Re:u_32 result truncated to u_16

Hi Ed,
Thank you for your very clear explanation.
rgds, Wilfried
www.mestdagh.biz
 

{smallsort}

Re:u_32 result truncated to u_16

It's good to hear that it helped.
I enjoyed your web site but was puzzled by one item. What does the word
'frozen' mean in 'frozen ATPL'?
. Ed
Quote
Wilfried Mestdagh wrote in message
news: XXXX@XXXXX.COM ...
Hi Ed,

Thank you for your very clear explanation.

rgds, Wilfried
www.mestdagh.biz
 

Re:u_32 result truncated to u_16

Hi Ed,
Quote
I enjoyed your web site but was puzzled by one item. What does the word
'frozen' mean in 'frozen ATPL'?
That's the page from my oldest son, he's the pilot :) ATPL stand for
Airlines Transport Pilot License, and they call it 'frozen' if only a
minimum required training is done on multi engine.
He should update his page because he is far above this stage. It is over
with the air-trouble and is now certificated on several Boeing airplanes
as first officer pilot, flying around the world :)
rgds, Wilfried
www.mestdagh.biz