Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: The result of casting a negative integer to a byte

Tags:

c#

I was a looking at the source code of a project, and I noticed the following statement (both keyByte and codedByte are of type byte):

return (byte)(keyByte - codedByte);

I'm trying now to understand what would the result be in cases where keyByte is smaller than codedByte, which results in a negative integer.

After some experiments to understand the result of casting a negative integer which has a value in the range [-255 : -1], I got the following results:

byte result = (byte) (-6);  // result = 250
byte result = (byte) (-50); // result = 206
byte result = (byte) (-17); // result = 239
byte result = (byte) (-20); // result = 236

So, provided that -256 < a < 0 , I was able to determine the result by:

result = 256 + a;

My question is: should I always expect this to be the case?

like image 350
mota Avatar asked May 13 '12 20:05

mota


People also ask

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 the full name of C?

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 is scanf () in C?

In C programming language, scanf is a function that stands for Scan Formatted String. It reads data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Yes, that will always be the case (i.e. it is not simply dependent on your environment or compiler, but is defined as part of the C# language spec). See http://msdn.microsoft.com/en-us/library/aa691349(v=vs.71).aspx:

In an unchecked context, the result is truncated by discarding any high-order bits that do not fit in the destination type.

The next question is, if you take away the high-order bits of a negative int between -256 and -1, and read it as a byte, what do you get? This is what you've already discovered through experimentation: it is 256 + x.

Note that endianness does not matter because we're discarding the high-order (or most significant) bits, not the "first" 24 bits. So regardless of which end we took it from, we're left with the least significant byte that made up that int.

like image 98
Tim S. Avatar answered Oct 04 '22 02:10

Tim S.