Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ulong overflow / underflow somehow allowed

I'm a little confused about the behavior of the ulong type. I understand it to be a 64-bit integer used for positive numbers (max value 18,446,744,073,709,551,615). I just started using it because I need really big positive numbers, but there's some strange behavior I don't understand around potential negative numbers.

ulong big = 1000000;    //Perfectly legal

ulong negative = -1;    //"Constant value "-1" cannot be converted to a ulong" Makes sense


//Attempt to sneak around the -1 as a constant compile-time error
int negativeInt = -1;
ulong negativeUlongFail = negativeInt;    //"Cannot implicitly convert 'int' to 'ulong'.
                                 //An explicit conversion exists (are you missing a cast?)"
//So I add casting
ulong negativeUlong = (ulong)negativeInt;    //But this yields 18446744073709551615

//Try and get a negative number indirectly
ulong number = 0;
number = number - 10;  //now number is 18446744073709551615 but no underflow error

What's going on? How come some underflow errors are caught and others aren't and don't even throw exceptions? Can I detect these?

I focused on getting negative numbers by underflowing, but I've seen similar things when getting numbers bigger than the max value and overflowing.

I'm not sure if they're technically Errors or Exceptions, so please forgive me if I used incorrect terminology

like image 407
SleekPanther Avatar asked Jul 23 '17 23:07

SleekPanther


People also ask

What is C full form?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is main () in C?

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.

What is C in coding language?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


Video Answer


1 Answers

if you want the underflow (or overflow) exception to be thrown, try this instead :-

checked
{
    ulong number = 0;
    number = number - 10;
}

by default the exception gets swallowed (it runs in unchecked).

see official documentation

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked

like image 130
Keith Nicholas Avatar answered Oct 20 '22 02:10

Keith Nicholas