i want to Not16 bits. but the answer is wrong.Why? [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to get 16 bits from Listbox and Not those bits. but I get wrong answer can anybody help me what's going wrong?
Input = 1101010101110101
Output = -1101010101110102
the expected result is : 0010101010001010
here is my code.
public static long NotInputEqualWhichBit(String str)
{
long Input = (Int64.Parse(str));
long notInput = ~Input;
return notInput;
}
and i called this method bellow
string listBoxLine = ListBox_content.Items[2].ToString();
long res2 = checkLogic_Class.NotInputEqualWhichBit(listBoxLine);
MessageBox.Show(res2 +"");

Your question is a little vague, so my answer will be too.
You are parsing a binary string as an int64. Basically, you're converting a 11 (string, but to you it is the binary value of three) into an 11 (decimal value of eleven).
If your question is about how a "2" digit can show up in your result, then this is your answer: when you convert your input to a int64, the conversion assumes that you're using decimal notation, not binary. Therefore, it interprets a different mathematical value.
Decimal notations allow for more than just 0 and 1, so that's why the 2 shows up.
If that's not your question, continue reading.
In C# (among other languages), the first bit that you find in an integer is called the "signed" bit. If it is 0, your integer is positive. If it is 1, your integer is negative.
Because you are inverting every bit, you are also flipping the signed bit; therefore turning your value into a negative number.
Change the following line:
long Input = (Int64.Parse(str));
Into the following:
ulong Input = (Convert.ToUInt64(str));
This is an unsigned long. Simply put, it does not have a signed bit and therefore cannot turn into a negative number.

I think the most important issue is that you want to make certain you have 16 bit and invert these. That is done by typecasting to ushort.
ushort i = 5;
Console.WriteLine((ushort)~i);

Related

c# adding money together removes the 0 at the end [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In c# when adding two decimals the program will automatically get rid of the number 0 after the decimal places.
For example adding 0.50 to 1.20 will produce the answer of 1.7 and this is annoying because i need to display this answer in terms of money.
Is there a way to prevent this?
If you want to display your Decimal with two decimal places, please use :
myDecimal.ToString("N2");
You may want to take a look at Standard Numeric Format Strings for more information.
decimal d = 0.50m;
decimal d1 = 1.20m;
Console.Write(d+d1);
Please find this Post
I'm not sure about if you mean this, but you can try the toString() method in currency format this way:
double number = 1.2;
string numberCurrency = number.ToString("C");
Console.WriteLine(numberCurrency); //this prints "1.20"
I recommend you to read this https://msdn.microsoft.com/es-es/library/kfsatb94(v=vs.110).aspx

Left-pad a floating point number without losing trailing zeros after decimal point [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am attempting to write out to a fixed format text file and am having some trouble formatting the numbers properly. It needs to be padded with leading zeros and an implied decimal.
Example:
43.80
The output would need to be:
0000004380
So far, I have attempted to convert the double to a string, replace the decimal, and then pad with "0".
((amount.ToString()).Replace(".","")).PadLeft(10, '0')
The problem with this is when the number ends with zeros. The above example comes out as:
0000000438
Any suggestions?
decimal value = 34.80M;
int intVal = (int)(value * 100);
return intVal.ToString("0000000000");
You could try:
((string.Format("{0:0.00}", amount)).Replace(".","")).PadLeft(10, '0')
That way you don't lose the 2nd decimal place when the value is 0.
You can pass a .ToString() format string in as described in this article:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Does the incoming value always have two decimal places? If so, you could multiply by 100 to get rid of the decimal, and then to .ToString("D10")
(Int)(43.80 * 100).ToString("D10")
This should do the job : $"{(int)(amount * 100):D010}"

how do i display how many digits the number has? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

What is the cheapest way in .net to determine enumeration-vs-number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to determine whether a given string is an enumeration value or not. It is rather common that the string should be a decimal value. My question is whether it takes longer to do an Enum.TryParse() on the string or to do a regex check that it is a decimal value or do a Int32.TryParse(). If the string is not a numeric (unsigned) decimal value, it is highly likely, but not 100% guaranteed, that the string is an enum value.
This check is made many many times. I want to get the lowest average cost.
You should profile your code to determine if this is actually a problem. If not, just do whatever gives you the most clear, concise (working) code. If it is a problem, this is probably a good way to pick which approach to use:
bool probablyANumber = char.IsDigit(myStr[0]);
This should work well because enums can't start with digits, while numbers usually do (with exceptions like "-1" and " 1 "). Whether this is an improvement or just makes things worse depends on your data set.
It's worth noting that enums can be parsed from numbers, so the behavior of Enum.TryParse may not be what you were expecting.
enum MyEnum
{
Zero,
One,
Two,
Ten = 10
}
MyEnum e;
Enum.TryParse<MyEnum>("10", out e);
// success; e == MyEnum.Ten
MyEnum e;
Enum.TryParse<MyEnum>("11", out e);
// success; e == (MyEnum)11
The fastest? Put the string values in a HashSet. HashSet.Contains()

C# Converting string to int fails [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am working on a little project, that at a certain point need to convert a string to int.
I tried the following things:
//first try
value1 = int.TryParse(value[0].tostring(), out i)
//second
value1 = Convert.ToInt32(value[0].tostring())
//third
value1 = int.Parse(value[0].tostring())
I even wrote my own conversion method because I was at a loss.
The values I am trying to convert are queried from a MySQL database.
Thank for your help
EDIT:
I know tryparse should have 2 params.
And the error iam getting is a formatexception
Input string was not in a correct format.
I got that on all the tries.
the value in my test case is 2500
Keep in mind that that number is received from a db
I tried the above snippets while using a hard coded value. And that works fine.
EDIT 2:
//http://imgur.com/NSyg2rJ
One:
The signature of int.TryParse() is incorrect:
int result = 0;
bool parsedSuccessfully = Int32.TryParse(value[0].ToString(), out result);
//If successful, result will hold the value.
Two:
The signature of Convert.ToInt32() is correct, minus the case of the .ToString() method. This is failing because value or value[0] is null, or it cannot convert something like the string "NotAnInt" to an int.
Three:
The signature of int.Parse() is correct, but is failing for the same reasons as two.
Try this -
int value1;
if (Int.TryParse(value[0].ToString(), out value1))
{
//conversion successful
}

Categories

Resources