Hi Guys
NP72 Data Lost
http://www.java2s.com/Code/CSharp/Language-Basics/Variable-Definition.htm
I got the following program from the above website. When there is a data lost particular number is output. For example:
b after assigning 257: 1 -- data lost.
s after assigning 64000: -1536 -- data lost.
u after assigning -12: 4294967284 -- data lost.
Are these numbers randomly displayed or any particular method to calculate it?
Anyone knows please explain.
Thank you
using System;
public class CastDemo
{
public static void
{
double x, y;
byte b;
int i;
char ch;
uint u;
short s;
long l;
x = 10.0;
y = 3.0;
// cast an int into a double
i = (int)(x / y); // cast double to int, fractional component lost
Console.WriteLine("Integer outcome of x / y: " + i);
Console.WriteLine();
// cast an int into a byte, no data lost
i = 255;
b = (byte)i;
Console.WriteLine("b after assigning 255: " + b +
" -- no data lost.");
// cast an int into a byte, data lost
i = 257;
b = (byte)i;
Console.WriteLine("b after assigning 257: " + b +
" -- data lost.");
Console.WriteLine();
// cast a uint into a short, no data lost
u = 32000;
s = (short)u;
Console.WriteLine("s after assigning 32000: " + s +
" -- no data lost.");
// cast a uint into a short, data lost
u = 64000;
s = (short)u;
Console.WriteLine("s after assigning 64000: " + s +
" -- data lost.");
Console.WriteLine();
// cast a long into a uint, no data lost
l = 64000;
u = (uint)l;
Console.WriteLine("u after assigning 64000: " + u +
" -- no data lost.");
// cast a long into a uint, data lost
l = -12;
u = (uint)l;
Console.WriteLine("u after assigning -12: " + u +
" -- data lost.");
Console.WriteLine();
// cast an int into a char
b = 88; // ASCII code for X
ch = (char)b;
Console.WriteLine("ch after assigning 88: " + ch);
}
}
/*
Integer outcome of x / y: 3
b after assigning 255: 255 -- no data lost.
b after assigning 257: 1 -- data lost.
s after assigning 32000: 32000 -- no data lost.
s after assigning 64000: -1536 -- data lost.
u after assigning 64000: 64000 -- no data lost.
u after assigning -12: 4294967284 -- data lost.
ch after assigning 88: X
Press any key to continue . . .
*/
Posted Dec 24, 2007, 8:09 PM
Thank you, Alan.
AlanPosted Dec 24, 2007, 7:32 PM
Multiplication is not actually defined for 'byte' (or for that matter 'short') in C#. Instead, the byte values are first 'promoted' to ints and the ints are then multiplied.
This means that b * b actually gives an int value. It therefore has to be cast back to a byte before it can be assigned to a byte variable, remembering that ints have a much bigger range than bytes.
The only situation where a cast is not needed is where you're assigning an int constant to a byte variable in a line such as :
b = 10; // 10 is an int literal i.e. a constant
The C# compiler will allow this as long as the constant is within the possible range of a byte i.e. between 0 and 255 inclusive.
Posted Dec 24, 2007, 6:44 PM
I got the following program from the above website. Variable b is declared as a byte and answer will also be in byte, so why casting needed. Anyone knows please explain the reason.
using System;
public class PromDemo
{
public static voidMain ()
{
byte b;
b = 10;
b = (byte)(b * b); // cast needed!!
Console.WriteLine("b: " + b);
}
}
/*
b: 100
Press any key to continue . . .
*/
Posted Dec 20, 2007, 1:35 PM
Thank you for your help, Alan.
AlanPosted Dec 20, 2007, 12:59 PM
No, they're not random answers - they're always the same.
However, to understand how the answers are arrived at, you need to understand the underlying bit representation of the numbers involved.
If you consider the int, 257, this looks like this in binary:
00000000 00000000 00000001 00000001
because there are 4 bytes of 8 bits each and 257 = 256 + 1
If you assign this to a byte variable (only one byte), then the three most significant bytes are discarded and you're left with just:
00000001
which is why the answer is 1.
If you've got the patience, it would be possible to understand the answers for the other assigments by considering their bit represenations remembering that short is 2 bytes, uint 4 bytes and long 8 bytes.
You also need to remember that, for signed types, the most significant bit is the sign bit and, if this is 1, the number is regarded as negative. Its value is then computed according to two's complement notation (flip all bits and add one, ignoring overflow). See this link if you want to know more:
http://en.wikipedia.org/wiki/Two's_complement