- 1

Help me on check the upper and lower limits of integer I need codes thank you

26th Jan 2021, 6:23 AM
Chararaat
Chararaat - avatar
7 ответов
+ 2
The integer limits are reported by the int object as properties. Lowest integer: int lower = int.MinValue; Highest integer: int upper = int.MaxValue;
26th Jan 2021, 6:32 AM
Brian
Brian - avatar
+ 2
I see the problem. When printing char.MaxValue in Console.WriteLine() it prints '?' instead of 127. That happens because the string + operator interprets char.MaxValue as a non-printable character (DEL) instead of an integer value. To see the integer value you can cast it as (int)char.MaxValue.
26th Jan 2021, 6:46 AM
Brian
Brian - avatar
0
https://code.sololearn.com/chPW6R1eXlaY/?ref=app that's my program but the bits contain char data types is not correct in the output can you help me to get it
26th Jan 2021, 6:34 AM
Chararaat
Chararaat - avatar
0
The number 21 should be 8 on output
26th Jan 2021, 3:26 PM
Chararaat
Chararaat - avatar
0
Here are two ways you can determine the number of bits. Both require having the highest bit set to 1. A negative number sets the highest bit regardless of datatype. Given that the highest bit is set, the first method shifts all bits rightward, counting the number of shifts until the whole value becomes zero. int bits = 0; char val = unchecked((char)-1); //unchecked ignores overflow do {bits++; val >>= 1; } while (val>0); Console.WriteLine("The bits contained in char data type: " + bits ); The second method calculates the number of bits mathematically by determining the magnitude of the highest bit. I use the logarithm function to calculate the base 2 exponent. Truncating the log to a whole number throws away any contributions from lower bits, leaving only the highest base 2 exponent. Then I translate it into a bit count by adding 1. Here is the code: Console.WriteLine("The bits contained in char data type: " + ((int)Math.Log(unchecked((char)-1), 2) + 1));
31st Jan 2021, 3:05 PM
Brian
Brian - avatar
0
I should point out that if you use the logarithm technique be careful to pass the argument as unsigned value. Here is how to show the number of bits used by the sbyte datatype by casting it into its equivalent unsigned type: Console.WriteLine("The bits contained in sbyte data type: " + ((int)Math.Log(unchecked((byte)(sbyte)-1), 2) + 1));
31st Jan 2021, 3:41 PM
Brian
Brian - avatar
0
Now that you have seen a couple of empirical methods to count bits of a type, here is a simpler solution. Get the number of bytes by using the sizeof operator, then multiply by 8 bits per byte. Console.WriteLine("The bits contained in char data type: " + 8*sizeof (char));
31st Jan 2021, 9:06 PM
Brian
Brian - avatar