Replies: 28 comments
-
Its twos complement representation. And thats also a common way to store signed numbers in hardware level. Because its simpler to implement. I think the reason it doesnt work for you is because that number magnitude is higher than short range, it then tries to cast value to short but there is arithmetic overflow in evaluating constant number. You can either go to advanced project settings to disable check for arithmetic overflow (warning: it affects entire project and can change behavior of your program. Make sure there is no unintended arithmetic overflow anywhere hapening in your project) https://msdn.microsoft.com/en-us/library/s4wcexbc.aspx Or explicitly write this. Which is better because it doesnt affect rest of your program.
|
Beta Was this translation helpful? Give feedback.
-
That doesn't help. That option is disabled by default, but it doesn't apply to constant values. |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary |
Beta Was this translation helpful? Give feedback.
-
All numeric literals in C# are nonnegative. A binary number is a nonnegative value specified in base 2. It is not "two's complement", as it cannot be negative. If you attempt to produce the bitwise representation of a negative number, you end up writing a number larger than can be represented as an |
Beta Was this translation helpful? Give feedback.
-
@gafter |
Beta Was this translation helpful? Give feedback.
-
11111111 is a number, not a representation. I don’t think we want numbers that are positive as written to magically become negative when run. |
Beta Was this translation helpful? Give feedback.
-
This is the binary system. It has no sign beacuase transistors can only be on or off. All of the programming world is represented as 0s and 1s eventually. |
Beta Was this translation helpful? Give feedback.
-
By the way, I don't want to remove the -ve sign. It makes things easy when one knows a +ve no. and want to negate it. But things get ugly when in the opposite situation, when one has the -ve binary no in first place! |
Beta Was this translation helpful? Give feedback.
-
If you try:
This is how binary system works. |
Beta Was this translation helpful? Give feedback.
-
I have an existing proposal for this: #717 The existing concern is the potentially Confusing behavior around overload resolution |
Beta Was this translation helpful? Give feedback.
-
I don't see any confusion. If var is used, the number can be assumed to be the shortest positve type, otherwise the programmer should clear his intentions. |
Beta Was this translation helpful? Give feedback.
-
It cannot, as that is a breaking change. I'm currently on my phone, but can provide better examples later tonight after I get home. |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem The C# programming language and the Binary numbers are specified in a C# binary literal using Unicode characters, not bits using two's complement notation. The rightmost digit position is a one, the next position is a two, and so on. There are no negative positions. No matter what base you are specifying your number in (16, 10, 8, or 2), without a negative sign the number isn't negative. That's just the way numbers are specified in different bases. We don't have a notation to let you specify the representation of the number you want. In any case, we could not change this even if we wanted to. It would cause a breaking change in overload resolution. |
Beta Was this translation helpful? Give feedback.
-
@gafter: This ssue is related to the same area: |
Beta Was this translation helpful? Give feedback.
-
Yes, a new notation for two's complement number would address this. |
Beta Was this translation helpful? Give feedback.
-
He is actually averting a significantly more complicated set of problems in the future which the "simple" (myopic) change would cause. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure I like this idea. Whilst 2's complement is near-ubiquitous these days, it's still an CPU-implementation detail and, as such, should not be of concern except in real edge cases. Do such edge cases really justify a new language feature? |
Beta Was this translation helpful? Give feedback.
-
@DavidArno |
Beta Was this translation helpful? Give feedback.
-
I'm not sure what do you mean by and what is stopping you from doing simple If you have difficulties solving your problem it maybe better to directly address that instead of requesting a feature and hoping for it to be released. even if you convince the team to make this happen, its going to take some time. I guess you want to solve your problem sooner than this. |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem,
For handling bit-mapped values. In every case I can think of, those values are unsigned numbers. And there is already very good support for bitmapped operations in the language. It's trying to mix bit-mapped values and negative numbers that I see as the extreme edge case and that therefore doesn't justify language support. Regarding your use-case of uint value = 0b11111111111111111111111111111111;
var value2 = (int)value;
Console.WriteLine(value2); // prints -1 Or you can just use unchecked
{
var value = (int)0b11111111111111111111111111111111u;
Console.WriteLine(value); // prints -1
} |
Beta Was this translation helpful? Give feedback.
-
They are just abbreviations for negative and positive, respectively. |
Beta Was this translation helpful? Give feedback.
-
Apparently |
Beta Was this translation helpful? Give feedback.
-
It's good old RAS Syndrome in action. |
Beta Was this translation helpful? Give feedback.
-
Ha! Never knew the term for that! |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary |
Beta Was this translation helpful? Give feedback.
-
+ve and -ve are famous betwwen engineers. |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem
Is that so? Still, I see no reason to use them in issue title especialy short as this one. |
Beta Was this translation helpful? Give feedback.
-
I think software engineers are far different than hardware engineers. 😄 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
0b1111111111111111 is the representation of -1 as
Convert.ToString((short)-1, 2)
shows, but C# refuses this code:
short x = 0b1111111111111111;
It only accepts:
short x = - 0b1;
Why can't we use the exact binary representation?
11111111 is the binary number that can represent 255 for Byte , or -1 for SByte. C# and VB.NET can take this in consederation. Or at least give it a new symbole: 0nb1111_1111.
I don't want to remove the -ve sign. It makes things easy when one knows a +ve no. and want to negate it. But things get ugly when in the opposite situation, when one has the -ve binary no in first place!
Beta Was this translation helpful? Give feedback.
All reactions