Forums Register Login

About cast of int

+Pie Number of slices to send: Send
As we all know , a literal integer (such as 27) is always implicitly an int. add two bytes together and you'll get an int

for example:

⑴ byte b = 27; // complie ok!

⑵ byte b = 3;
byte c = 8;
byte d = b + c; //compile error !

why in ⑴ the compiler automatically narrows the literal value to a byte
but in ⑵ the complier won't?
+Pie Number of slices to send: Send
Hi,

The compiler does narrow the byte b = 3; as it is doing in line 1. However the last line of your code appears to be the real problem here because you are trying to add to bytes which will result in an int so you cannot assign it to byte type variable. Looking at your code, it seems you might get a compile error in line 2 because you define variable b which has already been defined in line 1.

Regards
Paul.
+Pie Number of slices to send: Send
thank you ,Paul.

what I don't understand it is (byte b = 3) 3 is an int , but we can assign it to byte type variable .

and byte + byte is an int too,why we cannot assign it to byte type variable?
+Pie Number of slices to send: Send
@jack,


byte b = 3;
byte c = 8;
byte d = b + c; //compile error !


because result of an expression involving int sized or smaller is always int.
So if you add two byte result is int.
if you divide short by int result is int and so on.

you can correct this code as
byte d=(byte)(b+c);
1
+Pie Number of slices to send: Send
Hi Jack,

the problem here is that for the compiler it makes a difference if you assign an integer literal like "3" or if you assign an expression like "b + c". For you it may be obvious that "b + c" in this could be stored into a byte variable without loss of information but for the compiler the value of such an expression isn't known at compile time so it has to assume that the expression "b + c" could lead to an overflow if you try to put it in a byte variable. Therefore you have to explicitly cast it to a byte telling the compiler that you know there could be loss of precision and that's OK for you


Marco
+Pie Number of slices to send: Send
Maybe I didn't get the point, Jack.
Anyway, Pawni Jain is right. In addition I can add that when declaring a variable like byte b = 3 (or - in case of byte type - with any other value <= 127) the compiler is smart enough to notice that this number will fit into 8-bit byte so it narrows this - otherwise int literal - implicitely to byte.

Regards.
Paul.
+Pie Number of slices to send: Send
Thank you all, and thank Marco

For you it may be obvious that "b + c" in this could be stored into a byte variable without loss of information but for the compiler the value of such an expression isn't known at compile time .

and i think this is the point.
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 734 times.
Similar Threads
casting
Cast conversion
Serious Conversion Problem - Please HELP
Operators Question
Type Conversion doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:50:58.