Maha

Maha

  • NA
  • 0
  • 320.3k

NP72 Data Lost

Dec 20 2007 12:12 PM

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 Main()

    {

        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 . . .

*/


Answers (5)