35 votes

Checking if a number is power of 2 or not

There's actually a bit hack for this : ...
bowmore's user avatar
  • 5,369
27 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
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
  • 74.9k
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
  • 5,137
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
  • 74.9k
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
  • 66.4k
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.3k
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.5k
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,408
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
  • 74.9k
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
  • 74.9k
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
11 votes

Create the English word for a number

You have quite a big bug there. In this part: ...
ChatterOne's user avatar
  • 2,805
11 votes

BigInteger class, supporting addition only

Abusing the use of macros I think there is no good reason for implementing to_int() and to_char() as macros - such an ...
KjMag's user avatar
  • 601
11 votes

Format an integer with thousands separator (recursive implementation)

ArtemyVysotsky has provided you with a improved solution, so I am just going to give some general advice: Do not use using namespace std; since it not only lowers ...
Ben Steffan's user avatar
  • 5,228
11 votes
Accepted

Haskell BigInt addition

Your code contains at least one bug, therefore this review is a little bit shorter than usual since you need to fix that bug either way. So let's instead have a look at testing and how you can find ...
Zeta's user avatar
  • 19.3k
11 votes
Accepted

Huge integer class using base 2^32 (was 256) follow up

I recommend including your internal headers first, before any standard headers. This helps expose any accidental dependencies that make it hard to use your types in another program. So in HugeInt....
Toby Speight's user avatar
  • 74.9k
11 votes

Find all ways to express each of a list of even numbers as the sum of 2 positive, even numbers

You can divide any even number A into 2 (even) and A-2 (even). Do you want all the solutions instead? There's infinitely many. Negative numbers can also be even. Are we talking about just natural ...
slepic's user avatar
  • 5,437
10 votes
Accepted

Count number of bits to convert one integer to another integer

cmn_d = n1 & n2 all_d = n1 | n2 diff = all_d - cmn_d There is an operator for that: diff = n1 ^ n2 Having found the ...
Peter Taylor's user avatar
  • 24.1k
10 votes

Checking if each char in a string is a decimal digit

Use the standard library Pass by const& if you only want to read from a variable (and not copy it) Mark functions that should not throw ...
Rakete1111's user avatar
  • 2,562

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