I am unable to grasp the idea of either addition operator or short data-type.  It's said that;  short a = 1; short b = 2; short c = a + b;   which will not compile because addition operator always cast short, chart, byte data-types to int and I understand this. But this;  short c = 1 + 2;   works totally fine. So, if addition operator auto converts short to int and then apply the result (where result ofcourse will be an int), why this works fine?  Edit: This question is not duplicate of  since I understand the conversions process. Also, the question talks about conversions of data types where as my question relates to int literals.
4641
I have a question about the primitive type short in Java. I am using JDK 1.6.   If I have the following:   short a = 2; short b = 3; short c = a + b;   the compiler does not want to compile - it says that it "cannot convert from int to short" and suggests that I make a cast to short, so this:  short c = (short) (a + b);   really works. But my question is why do I need to cast? The values of a and b are in the range of short - the range of short values is {-32,768, 32767}. I also need to cast when I want to perform the operations -, *, / (I haven't checked for others).  If I do the same for primitive type int, I do not need to cast aa+bb to int. The following works fine:  int aa = 2; int bb = 3; int cc = aa +bb;   I discovered this while designing a class where I needed to add two variables of type short, and the compiler wanted me to make a cast. If I do this with two variables of type int, I don't need to cast.   A small remark: the same thing also happens with the primitive type byte. So, this works:  byte a = 2; byte b = 3; byte c = (byte) (a + b);   but this not:  byte a = 2; byte b = 3; byte c = a + b;   For long, float, double, and int, there is no need to cast. Only for short and byte values.
69127
I'm looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.  But are there any standards for C++?  I'm using Visual Studio 2008 on a 32-bit architecture. Here is what I get:  char  : 1 byte short : 2 bytes int   : 4 bytes long  : 4 bytes float : 4 bytes double: 8 bytes   I tried to find, without much success, reliable information stating the sizes of char, short, int, long, double, float (and other types I didn't think of) under different architectures and compilers.
40162
Please demonstrate how the ternary operator works with a regular if/else block. Example:  Boolean isValueBig = value &gt; 100 ? true : false;     Exact Duplicate:
95803
I'm having problems trying to overload the post increment operator in C#. Using integers we get the following results.  int n;  n = 10; Console.WriteLine(n); // 10 Console.WriteLine(n++); // 10 Console.WriteLine(n); // 11  n = 10; Console.WriteLine(n); // 10 Console.WriteLine(++n); // 11 Console.WriteLine(n); // 11   But, when I try it using classes, it looks like the objects are exchanged.  class Account {     public int Balance { get; set; }     public string Name { get; set; }      public Account(string name, int balance)     {         Balance = balance;         Name = name;     }      public override string ToString()     {         return Name + " " + Balance.ToString();     }      public static Account operator ++(Account a)     {         Account b = new Account("operator ++", a.Balance);         a.Balance += 1;         return b;     }      public static void Main()     {         Account a = new Account("original", 10);          Console.WriteLine(a); // "original 10"          Account b = a++;          Console.WriteLine(b); // "original 11", expected "operator ++ 10"         Console.WriteLine(a); // "operator ++ 10", expected "original 11"     } }   Debugging the application, the overloaded operator method, returns the new object with the old value (10) and the object that has been passed by reference, has the new value (11), but finally the objects are exchanged. Why is this happening?
35871
How does Python evaluate the expression 1+++2?  How many ever + I put in between, it is printing 3 as the answer. Please can anyone explain this behavior  And for 1--2 it is printing 3 and for 1---2 it is printing -1
80089
I have seen a solution that I don't get to work - I need to change data type from string to integer and I did what is shown i the image below Can someone point out what I do wrong?
36557
I am curious as to why an implicit cast fails in...  int? someValue = SomeCondition ? ResultOfSomeCalc() : null;   and why I have to perform an explicit cast instead  int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null;   It seems to me that the compiler has all the information it need to make an implicit casting decision, no?
82090
int i = 4; string text = "Player "; cout &lt;&lt; (text + i);   I'd like it to print Player 4.  The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
114980
See:   In many C flavoured languages, and some older languages like Fortran, one can use Pointers. As someone who has only really programmed in basic, javascript, and actionscript, can you explain to me what a Pointer is, and what it is most useful for? Thanks!
1464
introduces  which will allow the introduction of new literal syntax based on existing literals (int, hex, string, float) so that any type will be able to have a literal presentation.  Examples:  // imaginary numbers std::complex&lt;long double&gt; operator "" _i(long double d) // cooked form {      return std::complex&lt;long double&gt;(0, d);  } auto val = 3.14_i; // val = complex&lt;long double&gt;(0, 3.14)  // binary values int operator "" _B(const char*); // raw form int answer = 101010_B; // answer = 42  // std::string std::string operator "" _s(const char* str, size_t /*length*/)  {      return std::string(str);  }  auto hi = "hello"_s + " world"; // + works, "hello"_s is a string not a pointer  // units assert(1_kg == 2.2_lb); // give or take 0.00462262 pounds   At first glance this looks very cool but I'm wondering how applicable it really is, when I tried to think of having the suffixes _AD and _BC create dates I found that it's problematic due to operator order. 1974/01/06_AD would first evaluate 1974/01 (as plain ints) and only later the 06_AD (to say nothing of August and September having to be written without the 0 for octal reasons). This can be worked around by having the syntax be 1974-1/6_AD so that the operator evaluation order works but it's clunky.   So what my question boils down to is this, do you feel this feature will justify itself? What other literals would you like to define that will make your C++ code more readable?    Updated syntax to fit the final draft on June 2011
22739
Is this purely personal style, or are the generally accepted guidelines? Clearly, when dealing with numbers either $2 \cdot 2$ or $2 \times 2$ is needed, but within formulae where an absence of an operator implies multiplication, when should a \cdot be used? I tend to use it only sparingly if it aids in grouping. (Of course, barring the cases where it's required, e.g. a dot product.)
19898
Is there an efficient method of converting an integer into the written numbers, for example:  string Written = IntegerToWritten(21);   would return "Twenty One".  Is there any way of doing this that doesn't involve a massive look-up table?
32837
int temp = 0x5E; // in binary 0b1011110.   Is there such a way to check if bit 3 in temp is 1 or 0 without bit shifting and masking.  Just want to know if there is some built in function for this, or am I forced to write one myself.
115153
I know you can't rely on equality between double or decimal type values normally, but I'm wondering if 0 is a special case.  While I can understand imprecisions between 0.00000000000001 and 0.00000000000002, 0 itself seems pretty hard to mess up since it's just nothing.  If you're imprecise on nothing, it's not nothing anymore.  But I don't know much about this topic so it's not for me to say.  double x = 0.0; return (x == 0.0) ? true : false;   Will that always return true?
38145
I know, the question seems to be strange. Programmers sometimes think too much. Please read on... In C I use signed and unsigned integers a lot. I like the fact that the compiler warns me if I do things like assigning a signed integer to an unsigned variable. I get warnings if I compare signed with unsigned integers and much much more. I like these warnings. They help me to keep my code correct. Why don't we have the same luxury for floats? A square-root will definitely never return a negative number. There are other places as well where a negative float value has no meaning. Perfect candidate for an unsigned float. Btw - I'm not really keen about the single extra bit of precision that I could get by removing the sign bit from the floats. I'm super happy with floats as they are right now.  I'd just like to mark a float as unsigned sometimes and get the same kind of warnings that I get with integers. I'm not aware of any programming language that supports unsigned floating-point numbers. Any idea why they don't exist?  EDIT: I know that the x87 FPU has no instructions to deal with unsigned floats. Lets just use the signed float instructions. Misuse (e.g. going below zero) could be considered undefined behaviour in the same way as overflow of signed integers is undefined.
118343
I'd like to display the binary (or hexadecimal) representation of a floating point number.  I know how to convert by hand (using the method ), but I'm interested in seeing code samples that do the same.  Although I'm particularly interested in the C++ and Java solutions, I wonder if any languages make it particularly easy so I'm making this language agnostic.  I'd love to see some solutions in other languages.  EDIT: I've gotten good coverage of C, C++, C#, and Java.  Are there any alternative-language gurus out there who want to add to the list?
21733
EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text)     ? null     : Convert.ToInt32(employeeNumberTextBox.Text),  I often find myself wanting to do things like this (EmployeeNumber is a Nullable&lt;int&gt; as it's a property on a LINQ-to-SQL dbml object where the column allows NULL values).  Unfortunately, the compiler feels that  There is no implicit conversion between 'null' and 'int'  even though both types would be valid in an assignment operation to a nullable int on their own. Using the null coalescing operator is not an option as far as I can see because of the inline conversion that needs to happen on the .Text string if it's not null. As far as I know the only way to do this is to use an if statement and/or assign it in two steps.  In this particular case I find that very frustrating because I wanted to use the object initializer syntax and this assignment would be in the initialization block... Does anyone know a more elegant solution?