Hot answers tagged

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 ...
kojiro's user avatar
  • 1,767
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 <...
rolfl's user avatar
  • 97.3k
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 ...
Heslacher's user avatar
  • 50.3k
34 votes

Checking if a number is power of 2 or not

There's actually a bit hack for this : ...
bowmore's user avatar
  • 5,359
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 ...
h.j.k.'s user avatar
  • 19.2k
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: ...
Caridorc's user avatar
  • 27.4k
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: ...
Grajdeanu Alex's user avatar
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 ...
Toby Speight's user avatar
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. <...
R.. GitHub STOP HELPING ICE's user avatar
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, <...
Cody Gray - on strike's user avatar
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 ...
user2296177's user avatar
  • 3,533
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 ...
Matthieu M.'s user avatar
  • 4,764
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-...
Toby Speight's user avatar
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 ...
Quuxplusone's user avatar
  • 19.3k
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...
Nikita B's user avatar
  • 13k
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 ...
Edward's user avatar
  • 65.3k
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. ...
JS1's user avatar
  • 28.5k
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 ...
t3chb0t's user avatar
  • 44.1k
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. ...
Gareth Rees's user avatar
  • 49.6k
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(...
AJNeufeld's user avatar
  • 33.3k
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 <...
Sep Roland's user avatar
  • 4,308
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 ...
cat's user avatar
  • 977
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 ...
JS1's user avatar
  • 28.5k
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 ...
CodesInChaos's user avatar
  • 2,680
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 ...
Toby Speight's user avatar
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 ...
Toby Speight's user avatar
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 ...
Quill's user avatar
  • 11.9k
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 ...
Dan's user avatar
  • 3,763
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 (...
Der Kommissar's user avatar
13 votes
Accepted

C function to read only numeric values

Definition of function main The definition of the function main should start with ...
Andreas Wenzel's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible