# Questions tagged [integer]

Use this tag for questions about using, storing or manipulating integral values of all types and sizes, including concerns about overflow. Not for code that casually happens to use integers.

337 questions
Filter by
Sorted by
Tagged with
152 views

### Integer version of c++ standard library midpoint

I was extremely annoyed by the lengthy, edge-case-galore explanation of integer version of C++ standard library mindpoint implementation here, so I made my own simple 2's complement version. I present ...
930 views

### Convert Integer to String without using Java API library

I have written a Java program that converts an integer to String digit by digit and concatenate them together using the + operator without touching the Stock Java API library. I like to have feedback ...
219 views

### Football management game in C++

Recently, I have been working on a College Football Manager game, in which you coach a team from the ground up. I have successfully made it to where you can win games, lose games, and change your team ...
50 views

### How to simplify and optimize bitwise get/set operations on a large bit buffer in JavaScript?

tl;dr: How could you rewrite the buf_get and buf_set functions below to be most optimal? It took me a long time and a lot of ...
394 views

### How to optimize bitwise get/set/clear of ranges of bits on 8-bit integers in JavaScript?

I have been working for a few days on writing get, set, and clear bitwise functions in ...
55 views

### Converting an integer to a hexadecimal string representation in Java

I have rolled my own Java method for converting ints to the hexadecimal Strings: ...
946 views

### Swapping bytes of an integer in Java

I have this tiny function that expects an int and returns an int with the bytes swapped: ...
310 views

### Test if a number is within 2 of a multiple of 10

I have solved this exercise, but it looks really redundant. Is there a built-in function in Java or a cleaner way to caluclate mod of a value + deviation without multiple OR statements? If I had to ...
13k views

### Convert Persian digits to English numbers

I use the following code to convert ۱۲۳۴۵ to ‍12345, effectively transliterating the persian numbers into latin numbers: ...
97 views

### Return reversed integer - Java [closed]

The question is from Leetcode. Given a 32-bit signed integer, reverse digits of an integer. ...
72 views

...
42 views

### Large number stored as vector, increase performance

I made a code to initialize char vectors with random numbers representing very large numbers with more than 20.000 digits. The additions and multiplications are correct but my code is too slow (1 ...
104 views

### Reading and Storing Integer from Serial Monitor

Description I wanted to understand how integers can be written and stored inside variables without the use of Serial.parseInt(). I couldn't find any code examples about this. Perhaps this is the ...
467 views

### Signed integer-to-ascii x86_64 assembler macro

I have implemented an algorithm which converts a signed integer to its ASCII equivalent string with the corresponding sign as a first byte. I have tested it on several numbers and it worked correctly. ...
932 views

### Haskell functions to extract and reverse integers hiding in strings

There may not be a "right" way to do it, but I'm inexperienced in functional programming. I'd like some feedback on how the code can be written in a more idiomatic way. Here it is: ...
105 views

### HugeInt division using base 2^32 implementation

I previously submitted my code for a HugeInt class here. That version lacked a long division implementation. I have now implemented (long) division using Donald Knuth's algorithm D, following the ...
147 views

### Plotting the integer values in text format in matplotlib piechart

I have done a clustering algorithm and represented the results in a pie chart as shown below. ...
2k views

### Why are these functions slower than BigInteger's included methods?

I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
1k views

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

Thanks again to everyone for their very useful suggestions concerning a huge integer class I posted here several days ago. I have made many of the suggested changes, perhaps the biggest being in the ...
3k views

### Huge integer class using base 256

After a hiatus, I have returned to coding in C++. In an effort to learn some of the newer aspects of the language and for exercise and 'up-skilling', I am writing some simple classes and this ...
45 views

I'm implementing the following function for signed integer numbers: given endpoints of a closed interval lo and hi, and inputs <...
201 views

### Safe Integer Library in C++

I've designed a single-file safe integer library in C++. It catches undefined behavior prior to integer overflows or underflows and throws the respective exceptions. I intend this to be portable, to ...
6k views

### Fast 32x32 bit multiply on ARM M0

I have a time-critical calculation running on an ARM Cortex-M0. This is the fastest 32x32 bit -> 64 bit multiplication that I can come up with. The following C compiles almost 1-1 to assembly ...
318 views

### Codewars: Sum Strings

Sum Strings Codewars kata. It's supposed to be able to handle big integers.   Question Given the string representations of two integers, return the string representation of the sum of those integers. ...
62k views

### Simple method to detect int overflow

To detect int overflow/underflow in C, I use this code. What might be a simpler and more portable way of coding this (that is, fewer conditions)? Assume 2's ...
76 views

### converting bits binary to bcd

I wrote a code that requests user input and then converts binary to bcd then back to binary using bitwise operations. While I think my code is good I want to know if there is a better way to write the ...
458 views

### C - manually parsing char* into integer

I'm trying to parse a char* into an int without using atoi(). I walk through the string, ...
5k views

### Round to specified int

This accepts values from a user that needs to show up in a intervals of some number. In this case, we will say intervals of 5. Is there a better way to do this? ...
402 views

### Computational verification of Collatz conjecture using OpenCL

This code review request follows my previous request Computational verification of Collatz conjecture. Unlike the previous program (which was designed for the CPU), this code should run on modern GPUs....
2k views

### Simple Java program - Coding bat sumNumbers

The problem is stated as follows: Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: ...
816 views

### Function that adds commas between groups of 3 digits in a string

My program contains some very large integers and uses the GMP library. The output contains integers (probably small enough to fit into int). I would like to make ...
894 views

### Miller-Rabin Primality Test Using C++ and GMP

I'm currently writing a Miller-Rabin primality test using C++ and the GNU MP Bignum Library (GMP). As I've only started learning C++ about a day ago, I'm sure this code is riddled with errors and ...
167 views

### Wrapper for GMP in C++

I'm writing a C++ high precision library based on GMP, sample code(files which have to be added to project): ...
82 views

### Manually formatting numbers in Python

I was working on a little Python challenge that was a bit more difficult than expected: format any non-negative number so 1000 turns into ...
444 views

### Converting integers into Unary Notation

I have written a python script that has tallies representing digits 1-9 (0 is just ":" ) Unlike true Unary languages that are in the form 1^k. I represent values for n as per their decimal digits. The ...
86 views

### Python code to continue if a certain type is seen, otherwise try again to get valid input [closed]

I am very very new to programming, and this is my first day using Python. I want to check to see if an input is of type 'int' and to proceed with my program if the user has entered an 'int', ...
87 views

### Computational verification of Collatz conjecture using gmplib

Prerequisites The unsigned long type has a size of 64 bits. The following macros are defined. ...
303 views

### Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
1k views

### Binary Converting Code

I made a program that converts a decimal number given from the user, and converts it to a binary number. For example, if I entered 2, it would return 10. It then, counts the amount of ones and zeros ...
40 views

### Invert bits of a number in clojurescript

Doing a challenge to print the number that you would get if you convert a given number to binary, invert its bits, then convert back to decimal. I decided to give it a run in clojurescript (which I ...
3k views

### Find the product of digits at odd/even places

The 'score' of a race is given. We have to calculate the 'bonus points' to be given to the participant. The '...
34k views

### Incrementing Integers in Map

I have a HashMap<Token, Integer>, which counts occurrences of Token. Each time Token ...
331 views

### Using BigInt in JavaScript, write a factorial function that takes either a Number or BigInt and returns a BigInt

Is there anything missing or any pitfall when using BigInt? ...
82 views

### Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
193 views

### Python maintain precision for currency

Working with currencies I am aware it is not good to work with floating point values due to rounding and maintaining precision. Better is to work with integers. Would the following Currency class make ...
522 views

### Iterating over a range of integer ranges in Java

My use case is this: RangeDemo.java: ...
105 views

### Indeterminate-length big-endian to little-endian conversion in Python

I have a need to convert wire encoding (big-endian) to little-endian in Python. The conversion needs to happen as a separate function; I am not allowed to change the function that reads the byte ...
128 views

### Templated division by a power of 2

I've implemented a div_by_power_of_2() function, which lets me force the compiler to use left-shifting rather than proper division, in cases when the developer know ...