how to self-detect when it has overflowed the value (32 bit signed integer) of an int variable.  Can I add some conditional logic (an if statement) that breaks the loop when overflow is detected.  #include&lt;iostream&gt; using namespace std;  int factorial(int n){    if (n== 1){        return 1;    }     else {        return n * factorial(n-1);     }   } int main() {  for (int i = 0; i &lt; 20; i++) {     cout &lt;&lt; i+1 &lt;&lt;"!:" &lt;&lt; factorial(i+1) &lt;&lt; endl;  }     system("pause"); }
9588
I was writing a program in C++ to find all solutions of ab = c, where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and it ran a digit-counting routine each time on a, b and ab to check if the digits condition was satisfied.  However, spurious solutions can be generated when ab overflows the integer limit. I ended up checking for this using code like:  unsigned long b, c, c_test; ... c_test=c*b;         // Possible overflow if (c_test/b != c) {/* There has been an overflow*/} else c=c_test;      // No overflow   Is there a better way of testing for overflow? I know that some chips have an internal flag that is set when overflow occurs, but I've never seen it accessed through C or C++.    Beware that signed int overflow is undefined behaviour in C and C++, and thus you have to detect it without actually causing it. For signed int overflow before addition, see .
41877
Does Java have buffer overflows? If yes can you give me scenarios?
65440
When I type $\overset{\circ}{Z}^\lambda$ the lambda is placed slightly above and to the right of the circle. Is there a way to make it appear as a superscript on the Z? I tried $\overset{\circ}{Z^\lambda}$, but then the circle is too high and to the right.  As a bizarre aside: a friend uses the exact same code with no problem. However, when I type it into MathOverflow it looks the same as in my compiler (i.e. bad).
81821
In the  library, they are overloading the () operator.  Is this a convention in C++? For callbacks, etc.?  I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me.  Any insight as to the reason for this overload?
117193
Do we need to be careful when comparing a double value against zero?  if ( someAmount &lt;= 0){ ..... }
35169
Is there a performance difference between i++ and ++i if the resulting value is not used?
52567
When is it appropriate to use an unsigned variable over a signed one? What about in a for loop?  I hear a lot of opinions about this and I wanted to see if there was anything resembling a consensus.   for (unsigned int i = 0; i &lt; someThing.length(); i++) {       SomeThing var = someThing.at(i);       // You get the idea.   }   I know Java doesn't have unsigned values, and that must have been a concious decision on ' part.
36370
This is probably a simple answer but I can't find it.  I have a table with a column of integers and I want to ensure that when a row is inserted that the value in this column is greater than zero.  I could do this on the code side but thought it would be best to enforce it on the table.  Thanks!  I was in error with my last comment all is good now.
73742
What is the best way to handle large numeric inputs in C++ (for example 10^100)?   For algorithms I usually switch over to ruby and I sometimes use strings.  Any other good methods?
117745
I have a set of five boolean values. If more than one of these are true I want to excecute a particular function. What is the most elegant way you can think of that would allow me to check this condition in a single if() statement? Target language is C# but I'm interested in solutions in other languages as well (as long as we're not talking about specific built-in functions).   One interesting option is to store the booleans in a byte, do a right shift and compare with the original byte. Something like if(myByte &amp;&amp; (myByte &gt;&gt; 1)) But this would require converting the separate booleans to a byte (via a bitArray?) and that seems a bit (pun intended) clumsy... [edit]Sorry, that should have been if(myByte &amp; (myByte - 1)) [/edit]  Note: This is of course very close to the classical "population count", "sideways addition" or "Hamming weight" programming problem - but not quite the same. I don't need to know how many of the bits are set, only if it is more than one. My hope is that there is a much simpler way to accomplish this.
37655
In Bash, two integers can be compared using conditional expression     arg1 OP arg2      OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These   arithmetic binary operators return true if arg1 is equal to, not equal   to, less than, less than or equal to, greater than, or greater than or   equal to arg2, respectively. Arg1 and arg2 may be positive or negative   integers.   or arithmetic expression:     &lt;= &gt;= &lt; &gt; comparison      == != equality and inequality   Why do we have two different ways for comparing two integers? When to use which?  For example, [[ 3 -lt 2 ]] uses conditional expression, and (( 3 &lt; 2 )) uses arithmetic expression. Both returns 0 when the comparison is true  When comparing two integers, can these two methods always be used interchangeably? If yes, why does Bash have two methods rather than one?
109806
I'm using an istream which could be stringstream, ifstream or a user-defined stream type and I need to know if, in the case of an ifstream, it was not opened in binary mode (so I can throw an exception). I have tried the following method:  if ((_is.flags() &amp; ios::binary) == 0)     throw exception(...)   but no exception is ever thrown. The test fails in this case because _is.flags() returns 0x201 and ios::binary is 0x20. Is there a way to find out if a stream was opened in text mode?
57062
Today I needed a simple algorithm for checking if a number is a power of 2.  The algorithm needs to be:   Simple Correct for any ulong value.   I came up with this simple algorithm:  private bool IsPowerOfTwo(ulong number) {     if (number == 0)         return false;      for (ulong power = 1; power &gt; 0; power = power &lt;&lt; 1)     {         // This for loop used shifting for powers of 2, meaning         // that the value will become 0 after the last shift         // (from binary 1000...0000 to 0000...0000) then, the 'for'         // loop will break out.          if (power == number)             return true;         if (power &gt; number)             return false;     }     return false; }   But then I thought, how about checking if log2 x is an exactly round number? But when I checked for 2^63+1, Math.Log returned exactly 63 because of rounding. So I checked if 2 to the power 63 is equal to the original number - and it is, because the calculation is done in doubles and not in exact numbers:  private bool IsPowerOfTwo_2(ulong number) {     double log = Math.Log(number, 2);     double pow = Math.Pow(2, Math.Round(log));     return pow == number; }   This returned true for the given wrong value: 9223372036854775809.  Is there a better algorithm?
68536
In several modern programming languages (including C++, Java, and C#), the language allows  to occur at runtime without raising any kind of error condition.  For example, consider this (contrived) C# method, which does not account for the possibility of overflow/underflow.  (For brevity, the method also doesn't handle the case where the specified list is a null reference.)    //Returns the sum of the values in the specified list. private static int sumList(List&lt;int&gt; list) {     int sum = 0;     foreach (int listItem in list)     {         sum += listItem;     }     return sum; }   If this method is called as follows:  List&lt;int&gt; list = new List&lt;int&gt;(); list.Add(2000000000); list.Add(2000000000); int sum = sumList(list);   An overflow will occur in the sumList() method (because the int type in C# is a 32-bit signed integer, and the sum of the values in the list exceeds the value of the maximum 32-bit signed integer). The sum variable will have a value of -294967296 (not a value of 4000000000); this most likely is not what the (hypothetical) developer of the sumList method intended.  Obviously, there are various techniques that can be used by developers to avoid the possibility of integer overflow, such as using a type like Java's , or the  keyword and  compiler switch in C#.  However, the question that I'm interested in is why these languages were designed to by default allow integer overflows to happen in the first place, instead of, for example, raising an exception when an operation is performed at runtime that would result in an overflow.  It seems like such behavior would help avoid bugs in cases where a developer neglects to account for the possibility of overflow when writing code that performs an arithmetic operation that could result in overflow.  (These languages could have included something like an "unchecked" keyword that could designate a block where integer overflow is permitted to occur without an exception being raised, in those cases where that behavior is explicitly intended by the developer; C# actually .)  Does the answer simply boil down to performance -- the language designers didn't want their respective languages to default to having "slow" arithmetic integer operations where the runtime would need to do extra work to check whether an overflow occurred, on every applicable arithmetic operation -- and this performance consideration outweighed the value of avoiding "silent" failures in the case that an inadvertent overflow occurs?  Are there other reasons for this language design decision as well, other than performance considerations?
21288
It seems that   if (x=y) { .... }   instead of  if (x==y) { ... }    is a root of many evils.  Why don't all compilers mark it as error instead of a configurable warning?  I'm interested in finding out cases where the construct if (x=y) is useful.
76661
I'm reading an algorithms book and I came across a code example for a primality test. The problem is that I couldn't understand the condition for the :  public static boolean isPrime( int N ) {         if (N &lt; 2) return false;         for ( int i = 2; i * i &lt; N; i++ ) {             if ( N % i == 0 )                 return false;         }          return true; }   If we multiply i by itself we would miss a lot of numbers to check. So if we want to check if n=10 is prime or not, then we would quickly end the loop by i=3. I know that the idea is finish the loop as early as possible, but how do we guarantee that by using that condition we are actually checking all possible divisors and the rest that we omitted are never going to be divisors?  This condition seems quite common and popular but I really wasn't able to understand it or find an interpretation. I understood the condition i &lt; n/2 but not that one. I hope I'm not stupid!
9887
How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?  How do I view it?