66
votes
Checking if two numbers have the same sign
Why not compare the Booleans themselves?
return ((num1 < 0) == (num2 < 0));
This treats zero as "positive". For a stricter interpretation that considers ...
65
votes
Accepted
Checking if two numbers have the same sign
First up, your code has bugs. What if one of the numbers is 0? 0 should be positive, but your code treats it as negative in some tests, and positive in others. When comparing against 0 you should use <...
45
votes
Checking if two numbers have the same sign
Yes, there is a more elegant way to do this by
adding accessibility modifier to the method
use PascalCase casing for naming the method
naming the method ...
34
votes
26
votes
Accepted
Checking if a number is power of 2 or not
try-with-resources
Since Java 7, you should use try-with-resources on your Scanner for safe ...
25
votes
Accepted
Converting a string to an Integer in C
Avoid mysterious bit-shifting
return ~output + 1;
Is the same as:
return - (output + 1) + 1;
And as:
...
23
votes
Accepted
Create the English word for a number
There's an easier way.
We have the num2words module which can be easily installed via pip:
...
21
votes
Accepted
Huge integer class using base 256
General
I like the presentation. It's easy to read, with good use of whitespace and useful comments.
Width
It's inconvenient to have to recompile to use a larger width ...
21
votes
Standard C overflow-safe arithmetic functions
This code has a lot of problems.
#define err exit(1)
exit(1) is not a good way to terminate the program when trapping a bug. <...
19
votes
Accepted
Endianness conversion in C
First off, Jean-François is absolutely right: you cannot assume any particular bit widths for the built-in types, short, int, <...
19
votes
Checking if each char in a string is a decimal digit
In the cctype header, you have the std::isdigit(int) function. You can use this instead of your conditions that check if the ...
17
votes
Accepted
Implementation of C Standard Library Function ntohl()
Unless you wish to optimize the code, with specialized swappers for various hosts orders, you are doing it wrong.
I invite you to check The Byte Order Fallacy by Rob Pike. The punch line: the byte ...
17
votes
Standard C overflow-safe arithmetic functions
I'm pretty sure this causes undefined behaviour:
#define long int64_t
Even if I'm wrong, it's extremely confusing to readers, and will break any subsequently-...
16
votes
Fast 32x32 bit multiply on ARM M0
First of all, yes, CortexM0 lacks any way to do 32x32=64 multiplication in hardware. CortexM3 and CortexM4 have the umull instruction, which lets you do 32x32=64 ...
16
votes
Accepted
Convert a boxed integer to an nullable integer with potentially different type
You should probably add IConvertible restriction on T, since it is used by Convert.ChangeType...
16
votes
Checking if each char in a string is a decimal digit
The other reviews have hit most of the important points, so I'll just provide a single line alternative that uses the C++11 std::regex_match. That function looks ...
15
votes
Accepted
Detecting arithmetic overflow in C with NASM
Wrong flag
I believe you should be looking at the overflow flag instead of the carry flag, since all of your operands are signed values. On x86, the overflow flag is set if signed addition overflows. ...
15
votes
Convert a boxed integer to an nullable integer with potentially different type
I'm not sure if you understand what boxing means because you use an int in the example.
Boxing is the process of converting a value type to the type ...
15
votes
Accepted
'StrictInt' Python object class that prohibits casting numbers with non-integer components to `int`
Converting everything via a float means that you get the wrong result whenever the input cannot be represented exactly as a double-precision floating-point number. ...
15
votes
Accepted
Swapping bytes of an integer in Java
Historical Note
The very first comment on the OP's question -- long before any answers were posted -- was a question asked by myself, and it very pointedly asked:
Without using Integer.reverseBytes(...
15
votes
Accepted
Minimal `printf` for integer types in x86 assembly
If, for the sake of this exercise, codesize is the only thing that you care about, then I would dare suggest the following:
For divq10, not having to reload the constant will save 5 bytes, and using <...
14
votes
Accepted
Successful use of strtol() in C
Compiler errors
First of all, this code does not compile with the (IMO too lenient) command line of :
gcc -Wall -Werror main.c -o strtol
Instead it gives the ...
14
votes
Accepted
Counting the number of "on" bits in an int
The current implementation
Your implementation is correct, and takes \$O(n)\$ time, where \$n\$ is the number of bits in an int. One thing you can improve is the ...
14
votes
Checking if a number is power of 2 or not
It incorrectly returns false when the input is 1. \$2^0 = 1\$
Looping up to number is very ...
14
votes
Accepted
C++17 saturating integer (arithmetic) type library
Refactor the limits
There's quite a lot of repetition of std::numeric_limits<T>::max() and ...
14
votes
128-bit unsigned integer
std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use ...
13
votes
Arabic to Greek numeral converter, 1 - 10
Reviewing your current structure: your return ""; could be improved: it should / could be inside the switch statement as a ...
13
votes
Accepted
Arabic to Greek numeral converter, 1 - 10
It's great to see that you're using strict mode for strict compliance (which should really be the default), however you shouldn't use strict mode the way you are now. You've probably seen strict mode ...
13
votes
Accepted
Int to Enum Extension Method
As Jason Tyler said in a comment, this has a problem that [Flags] enumerations won't be properly handled.
I ran this in C# interactive, and it threw an exception (...
13
votes
Accepted
C function to read only numeric values
Definition of function main
The definition of the function main should start with
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
integer × 383c++ × 88
c × 79
java × 77
performance × 50
python × 41
bitwise × 41
strings × 40
beginner × 36
algorithm × 25
formatting × 25
c# × 23
reinventing-the-wheel × 22
parsing × 21
mathematics × 19
array × 18
converting × 18
assembly × 18
javascript × 16
python-3.x × 16
c++11 × 16
number-systems × 15
serialization × 14
floating-point × 12
interview-questions × 11