# 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.

308 questions
Filter by
Sorted by
Tagged with
2k views

### Simple calculation program in assembly MASM

I am a beginner in assembly. This program calculates: (Z+Z)+(X-Y) ...
625 views

### Concatenate three 16-bit integers in one 64-bit integer with C++

I am looking for an elegant alternative of the following code where wordXX contains always 16 bits: ...
851 views

### Portable byte order conversion

I'm trying to improve the portability of a file format converter, specifically to POSIX platforms other than Linux. I'd appreciate a review of the following functions used for converting between ...
4k views

### Python combine two integers to a third

We have three integers: A, B, C where the first (i.e. the most significant) digit of C is the first digit of A, the second digit of C is the first digit of B, the third digit of C is the second ...
111 views

### Safe, fully standards-following integer abs in C

Here's a bit tricky one, because AFAIK there is no existing platform, where conditionally compiled part of the code would actually get included by compiler, so it is entirely up to the human review to ...
95 views

### Multiple base numbers parser

This code is a Parser that parses numbers according to R5RS. #b1001 - binary #o2127 - octal #h02d - hexadecimal #d1231 - decimal 3923 - decimal It is working ...
59 views

### Turning an Integer into an array of ints

My goal here is pretty simple, I want to turn a number like 12435987234 into an array of integers in reverse order. So that would look like: ...
1k views

### Arabic to Greek numeral converter, 1 - 10

One of the things that's straight-forward in JavaScript is Unicode support. If you want to return ϝ (lowercase digamma, not a simple ...
296 views

### Convert a number into a different base and return as string

I wanted to write a function that would take a long long int argument as well as a base and it would convert that number into an equivalent number in a different ...
912 views

### Detecting arithmetic overflow in C with NASM

This snippet is about detecting whether the carry flag is set or cleared. I work on Mac OSX, so my snippet supports Mac only. First, we need a routine that does the job: func.s: ...
2k views

### Converting a string to an Integer in C

I'm learning C and one of the questions I've been asked is converting a string to an integer. The code I've written supports converting from a string in any base up to 16/hex to an integer. There is ...
5k 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 ...
486 views

### Recursive implementation of integer partition without re-arrangement

Problem: Integer partition without re-arrangement Input: An arrangement S of nonnegative numbers {s1, . . . , sn} and an integer ...
2k views

### Function to convert a decimal number as a string to hexadecimal

I am designing a cipher and need to convert between bases repeatedly in a loop. I have optimized everything else, but I'm not too familiar with C++ code, and and am trying to figure out how to make ...
2k views

### Diffie-Hellman in JavaScript

Diffie-Hellman is a key exchange that allows 2 people to share a symmetric key without interaction beforehand. First, a person shares an equation; in this case, we use: $$3^x \mod{17}$$ Next, each ...
306 views

### Implementing min of 3 numbers in Mic-1 code

I have the following algorithm to find a minimum of three input numbers: ...
4k views

### Java conversion of base 16 to base 64

I wrote a solution in Java to solve the problem here. I use BigInteger to convert first from base 16 to base 10, then base 10 to base 64. This seems quite ...
188 views

### All arithmetic operator functions (+, -, *, /, %) coded only using bitwise operators in C

Here's all the functions in C that does all the arithmetic operators without using arithmetic operators themselves, mostly using bitwise operators. Recursive functions and comparison operators are ...
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 ...
215 views

### Long arithmetic addition in JS

The implementation of the algorithm which adds two number in string form with arithmetic rules. ...
722 views

### Floor and Ceiling to specified int

This relates to Round to specified int but I'm separating these methods out since they're technically different. Is there a method that already performs a custom floor and ceiling to a specified ...
4k 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? ...
5k views

### Maximum product of 3 integers in an int array

I want to find the maximum product that can be obtained from any 3 integers in an integer array. The optimal solution has time complexity of $O(n)$ and space complexity of $O(1)$. I managed to ...
128 views

### Text representation for numbers with python

Simple function which returns a text representation for numbers in range [1...1000]. ...
7k views

### Numbers to byte-arrays and back

As the title explains, this is a series of extension methods that convert certain numeric types to and from byte-arrays, for certain actions which work better on byte-array types than numeric types. ...
2k views

### Two integers x & y

Given two int values, print whichever value is larger. However if the two values have the same remainder when divided by 5, then the print the smaller value. However, in all cases, if the two values ...
287 views

### Add one to a very large integer

I was asked to implement this in an interview a while back. I didn't pass the interview, mostly likely because it took me far too long to do. However, I'm interested to know how well implemented the ...
3k views

### Little/Big Endian conversion

I wanted to make sure that my code is properly converting between the two endians. Here is the code where I read in an integer: ...
19k views

### Checking if two numbers have the same sign

Is there a more elegant way than the following to check if two numbers have the same sign? ...
83 views

### Program to calculate smallest number with permutations divisible by a set of numbers

(EDIT: here is the follow up question) I have written a small program in C to calculate the smallest number where any of its permutations or the number itself is divisible by a set of given numbers. ...
5k views

### Number to german decimal format converter function [closed]

I have a function that formats a number (even a float) to a German decimal format. I got this function from here https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-...
13k views

### Hexadecimal number to decimal conversion

I am a semi-beginner in C++ and I have a project in C++ to convert a hexadecimal integer into decimal equivalent, but I extended it and now this program can also convert a fractional part. Logic I ...
516 views

### Recursive factorial calculations in Rust

I am trying to calculate very large factorials. I am looking to speed it up as 10000! takes approximately a minute and a half. ...
544 views

### C++ console program

I have been learning C++ programming. What you see here is all that I know how to do at this point. I look forward to learning more and I know there are better ways to accomplish a task such as ...
6k views

...
2k views

### Reverse binary representation of an int (only significant bits)

This based on this question in StackOverflow. The accepted answer uses Convert.ToString(int, base) to get the binary string, ...
144 views

### K&R atoi() variation

I'm currently learning C with The C Programming Language by K&R and this is my current exercise: Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array ...
1k views

### C - K&R getint() variation

I'm currently learning C with "The C Programming Language" by K&R. At the moment I'm at the chapter about pointers (since I come from Java, they are new to me). In the book is an example code for ...
1k views

### Tuple<int, int> replacement

I use the following structure in a web service where performance is critical. It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 ...
3k views

### Character to decimal conversion

This converts from character representation of integers to their decimal value: ...
6k views

### Simple function to verify if a number is integer

Sometimes, there's the need to verify if a given number is an integer or a decimal. Since JavaScript doesn't distinguish between both, I've made an extremelly basic function. ...
540 views

### Add two numbers stored as a list in F#

I am solving the following problem: You are given two lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two ...
2k views

### Function that adds two integer strings

I wrote this method that adds two integers in the form of strings together. This way I can add large integers without overflowing. How efficient is the code that I wrote? I am by no means a great ...
5k views

### Safe multiplication of two 64-bit signed integers

The function below implements safe multiplication of two 64-bit signed integers, preventing overflow from occurring: ...
97 views

### Method that returns an integer chosen by user

Can I make this: if (oneFour == 1 || oneFour == 2 || oneFour == 3 || oneFour == 4) shorter, especially if I'm gonna need a wider range of integers? Do I need to ...
18k views

### Counting words, letters, average word length, and letter frequency

I'm pretty sure my code is mostly correct. I think I'm having formatting errors more than anything. I keep receiving warnings about "double to float and int to float,possible loss of data. Here is ...
10k views

### Convert Persian digits to English numbers

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

### IntModulus long addition and subtraction

One of the remarks in this answer to my question concerning IntModulus says What's more problematic is that you don't have matching argument symmetry for add and ...