473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Multiplication for types smaller than int yields an int

When multiplying two sbyte, byte, short, ushort, the
result is an int.

It seems to me that the language should leave the result
alone, otherwise let the user decide whether to use a
checked expression.

Can someone offer any insight for this design decision?

Regards,

Javier

Please respond to the newsgroup so everybody can benefit
from the answer.
Nov 15 '05 #1
3 7169
Javier Estrada wrote:
When multiplying two sbyte, byte, short, ushort, the
result is an int.

It seems to me that the language should leave the result
alone, otherwise let the user decide whether to use a
checked expression.

Can someone offer any insight for this design decision?


IIRC, arithmetic operation results on 32 bit CPUs are always 32 bits, for
operands <= 32 bits.
BTW, in C++ we have a similar situation with the difference that the
conversion to the result type is implicit.

Regards,

Andreas

Nov 15 '05 #2
Javier Estrada wrote:
IIRC, arithmetic operation results on 32 bit CPUs are always 32
bits, for operands <= 32 bits.
BTW, in C++ we have a similar situation with the difference that the
conversion to the result type is implicit.
It seems to me that the compiler should hide the CPU
architecture.


Maybe, but that would be surprising for C++ programmers at which C# is
aimed. Moreover, some operations on byte or short values would overflow.
Consider the following situation:

byte x = 17;
byte y = 16;

byte result = x * y / 2;

x * y will yield a value greater than 255, but the division by 2 puts the
result back in the range of a byte. IIUC, then you would rather see result
holding an incorrect value?

Of course, you can argue similarly in the following situation:

uint x = 65537;
uint y = 65536;

uint result = x * y / 2;

Here, result will indeed hold an incorrect value, but most ex-C++
programmers will know that and convert the operands to ulong before applying
the operation.
When you say that in C++ the result type is implicit do
you mean implicitly and int or implicitly the type that
its supposed to be--from my description?


For integer operands <= 32 bits C++ will always calculate intermediate
results with 32 bits. However, in the end when the intermediate result is
assigned to a variable < 32 bits an explicit cast is not necessary. Some
compilers will issue a warning though (e.g. MSVC7.1 does so on warning level
4).

HTH,

Andreas

Nov 15 '05 #3
> For integer operands <= 32 bits C++ will always calculate intermediate
results with 32 bits. However, in the end when the intermediate result is
assigned to a variable < 32 bits an explicit cast is not necessary.


Sorry, that is of course wrong. C++ integer types do not have
guaranteed sizes. So, more correctly:

For integer operands <= int (i.e. char, short, int), C++ will always
calculate intermediate results with int. However, in the end when the
intermediate result is assigned to a variable < int an explicit cast
is not necessary.

On most (if not all) platforms, a C++ int has as many bits as the data
bus, i.e. 32 on PCs.

Regards,

Andreas
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Foster | last post by:
Hi, I'm having some difficulty using types which are defined in a base class inside a derived class. The problem crops up using template classes. The following test code encapsulates what I'd...
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
10
by: Noah Roberts | last post by:
I understand that different integer types might have time penalties because of word boundaries. Where can I get information clarifying this topic? A quick google didn't turn up much for me...nice...
3
by: badc0de4 | last post by:
Isn't it supposed that the addition 'works' modulo (TYPE_MAX + 1)? ~~~~~~~~~~ unsigned char a, b unsigned char c; a = b = 200; c = a + b; /* OK, c now has 144 */ ~~~~~~~~~~ unsigned char a,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.