C# .NET

[C#] uint, UInt16, UInt32 and UInt64 in C#

Code GGOON 2020. 9. 12. 16:51
반응형

C#에서 uint, UInt16, UInt32, UInt64 간의 차이점

 

uint, UInt16, UInt32 및 UInt64는 메모리에서의 용량 및 점유된 크기를 기반으로 값 범위를 갖는 부호없는 정수를 표현하는데 사용됩니다. 이러한 유형은 양수의 값으로 작동하고, 각각의 차이는 값의 범위가 다릅니다.

 

1) UInt16

  • UInt16은 16 비트(2 바이트) 부호없는 정수를 뜻합니다.
  • UInt16은 메모리에서 16 비트(2 바이트) 공간을 차지합니다. (16 비트 컴퓨터/ 산업용 PLC 등)
  • 2 바이트 부호없는 정수의 데이터 범위는 0 ~ 65535 입니다.

[사용예]

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //printing UInt16 capacity, type, MIN & MAX value
            Console.WriteLine("UInt16 occupies {0} bytes", sizeof(UInt16));
            Console.WriteLine("UInt16 type is: {0}", typeof(UInt16));
            Console.WriteLine("UInt16 MIN value: {0}", UInt16.MinValue);
            Console.WriteLine("UInt16 MAX value: {0}", UInt16.MaxValue);
            Console.WriteLine();

            //UInt16 variables
            UInt16 a = 12345;
            UInt16 b = 65000;
            Console.WriteLine("a = {0}, b = {1}", a, b);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output :

더보기

UInt16 occupies 2 bytes

UInt16 type is: System.UInt16

UInt16 MIN value: 0

UInt16 MAX value: 65535

 

a = 12345,

b = 65000

 

 

2) UInt32 / uint

uint는 UInt32의 별칭 이므로 uint와 UInt32는 동일한 유형입니다.

UInt32는 32 비트(4 바이트) 부호없는 정수는 나타냅니다.

UInt32는 메모리에서 32 비트(4 바이트) 공간을 차지합니다.

4 바이트 UInt32의 값 범위는 0 ~ 4294967295 입니다.

 

[사용예]

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //printing UInt32 capacity, type, MIN & MAX value
            Console.WriteLine("UInt32 occupies {0} bytes", sizeof(UInt32));
            Console.WriteLine("UInt32 type is: {0}", typeof(UInt32));
            Console.WriteLine("UInt32 MIN value: {0}", UInt32.MinValue);
            Console.WriteLine("UInt32 MAX value: {0}", UInt32.MaxValue);
            Console.WriteLine();

            //UInt32 variables
            UInt32 a = 289812345;
            UInt32 b = 90909;
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine();

            //printing uint capacity, type, MIN & MAX value
            Console.WriteLine("uint occupies {0} bytes", sizeof(uint));
            Console.WriteLine("uint type is: {0}", typeof(uint));
            Console.WriteLine("uint MIN value: {0}", uint.MinValue);
            Console.WriteLine("uint MAX value: {0}", uint.MaxValue);
            Console.WriteLine();

            //uint variables
            uint x = 289812345;
            uint y = 90909;
            Console.WriteLine("x = {0}, y = {1}", x, y);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output :

UInt32 occupies 4 bytes
UInt32 type is: System.UInt32
UInt32 MIN value: 0
UInt32 MAX value: 4294967295

a = 289812345, b = 90909

uint occupies 4 bytes
uint type is: System.UInt32
uint MIN value: 0
uint MAX value: 4294967295

x = 289812345, y = 90909

 

3) UInt64

UInt64는 64 비트(8 바이트) 부호없는 정수는 뜻합니다.

UInt64는 메모리에서 64 비트(8 바이트)의 용량을 차지합니다. (현재 대부분의 범용 CPU 레지스트 / 64비트 컴퓨터)

8 바이트 UInt64형의 값의 범위는 0 ~ 18446744073709551615 입니다.

 

[사용예]

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //printing UInt64 capacity, type, MIN & MAX value
            Console.WriteLine("UInt64 occupies {0} bytes", sizeof(UInt64));
            Console.WriteLine("UInt64 type is: {0}", typeof(UInt64));
            Console.WriteLine("UInt64 MIN value: {0}", UInt64.MinValue);
            Console.WriteLine("UInt64 MAX value: {0}", UInt64.MaxValue);
            Console.WriteLine();

            //UInt64 variables
            UInt64 a = 289818278722345;
            UInt64 b = 98983989;
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output :

UInt64 occupies 8 bytes
UInt64 type is: System.UInt64
UInt64 MIN value: 0
UInt64 MAX value: 18446744073709551615

a = 289818278722345, b = 98983989

 

반응형

'C# .NET' 카테고리의 다른 글

C# Foreach Index, Value  (0) 2020.10.08
Panel내부에서 Control 위치변경 ( Drag & Drop )  (0) 2019.12.11
DataTable Linq 데이터추출 예제  (0) 2019.12.03