- Use the
double
version and just cast back touint64_t
. - Use powers of 2 and abuses of shifting (x16 =
x << 4
) - Use divide and conquer style divide and conquer style
- Use the
double
version and just cast back touint64_t
. - Use powers of 2 and abuses of shifting (x16 =
x << 4
) - Use divide and conquer style
- Use the
double
version and just cast back touint64_t
. - Use powers of 2 and abuses of shifting (x16 =
x << 4
) - Use divide and conquer style
system is bad. Either use getchargetchar()
, set your IDE to pause after execution, or use a terminal when running a terminal program.
The typedeftypedef
for uint64uint64
should just be uint64_tuint64_t
from stdint.h
.
Your input reading should be checked. For example, std::cin >> choice;
can fail. It should beif (!(std::cin >> choice)) { /* handle this */ }
.
if (!(std::cin >> choice)) { /* handle this */ }
Similarly, sprintfsprintf()
can convert to hex for you (format llX
).
As an example, you could simplify your beastly hexidecimalToDecimal()
:
When arguments are NOT modified, they should be passed as constconst
references. This avoids the overhead of copying. (And it's part of a larger concept called const-correctness.)
If for some odd reason you don't want to do that, calculatePowercalculatePower()
can be optimized in many different ways.:
- Use the double
double
version and just cast back to uint64_tuint64_t
. - Use powers of 2 and abuses of shifting (x^16x16 = x << 4
x << 4
) - Use divide and conquer style
remainder = decimal % 2;
if (remainder == 0)
binary += '0';
else if (remainder == 1)
binary += '1';
remainder = decimal % 2; if (remainder == 0) binary += '0'; else if (remainder == 1) binary += '1';
As remaining can only be 0 <= remiainderremainder
<= 1, the else if
is unnecessary. I would just use:
You can use std::reverse (from algorithm<algorithm>
) rather than your manual reversal loop.
system is bad. Either use getchar, set your IDE to pause after execution, or use a terminal when running a terminal program.
The typedef for uint64 should just be uint64_t from stdint.h
Your input reading should be checked. For example, std::cin >> choice;
can fail. It should beif (!(std::cin >> choice)) { /* handle this */ }
.
Similarly, sprintf can convert to hex for you (format llX
).
As an example, you could simplify your beastly hexidecimalToDecimal
:
When arguments are NOT modified, they should be passed as const references. This avoids the overhead of copying. (And it's part of a larger concept called const-correctness.)
If for some odd reason you don't want to do that, calculatePower can be optimized in many different ways.
- Use the double version and just cast back to uint64_t.
- Use powers of 2 and abuses of shifting (x^16 = x << 4)
- Use divide and conquer style
remainder = decimal % 2;
if (remainder == 0)
binary += '0';
else if (remainder == 1)
binary += '1';
As remaining can only be 0 <= remiainder <= 1, the else if
is unnecessary. I would just use:
You can use std::reverse (from algorithm
) rather than your manual reversal loop.
system is bad. Either use getchar()
, set your IDE to pause after execution, or use a terminal when running a terminal program.
The typedef
for uint64
should just be uint64_t
from stdint.h
.
Your input reading should be checked. For example, std::cin >> choice;
can fail. It should be
if (!(std::cin >> choice)) { /* handle this */ }
Similarly, sprintf()
can convert to hex for you (format llX
).
As an example, you could simplify your beastly hexidecimalToDecimal()
:
When arguments are NOT modified, they should be passed as const
references. This avoids the overhead of copying. (And it's part of a larger concept called const-correctness.)
If for some odd reason you don't want to do that, calculatePower()
can be optimized in many different ways:
- Use the
double
version and just cast back touint64_t
. - Use powers of 2 and abuses of shifting (x16 =
x << 4
) - Use divide and conquer style
remainder = decimal % 2; if (remainder == 0) binary += '0'; else if (remainder == 1) binary += '1';
As remaining can only be 0 <= remainder
<= 1, the else if
is unnecessary. I would just use:
You can use std::reverse (from <algorithm>
) rather than your manual reversal loop.
When arguments are NOT modified, they should be passed as const references. This avoids the overhead of copying. (And it's part of a larger concept called const-correctness.)
When arguments are modified, they should be passed as const references. This avoids the overhead of copying. (And it's part of a larger concept called const-correctness.)
When arguments are NOT modified, they should be passed as const references. This avoids the overhead of copying. (And it's part of a larger concept called const-correctness.)