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.

Filter by
Sorted by
Tagged with
4
votes
3answers
332 views

Dividing Negative integer ranges

Context: My project has a hand-rolled ORM (... yeah ... I know) ... built around the MS DataSet objects that represent wrapped collections of rows of DBtables (... yeah ... I know) ... and which ...
3
votes
2answers
449 views

Converting bits to decimal (integer) string is so slow

I am implementing myself a BigInteger class. It can natively handle very very big digits in very short time. ...
1
vote
2answers
108 views

Finding shortest sequences of integers whose product is larger than n

Here is the task: For an integer n, return a list of the k length integers sequences whose product is larger than n but as close to n as possible (i.e. smallest larger than n number), and whose ...
3
votes
1answer
2k views

Unsigned integer division ARM Cortex-M0+ Assembly

I am writing a subroutine for unsigned integer division in Assembly. I will call the subroutine DIVU. Inputs: R1 will be the ...
1
vote
1answer
90 views

Translating binary data from a file into a very large base-ten integer

I'm making an experimental Java algorithm that translates binary data from a file into a (very large) base ten integer. I am using BigInteger since this number may ...
6
votes
2answers
213 views

Valid memory address in Python

I am looking for suggestions of improving this function to check whether a memory address is appropriate or not. ...
4
votes
2answers
110 views

Adding two binary-coded-decimal numbers

I want to make some VHDL code open source. But before I do, I want to make sure that it is as readable as possible. Things to improve upon could for example be naming and the use of comments. The ...
4
votes
3answers
6k views

Finding the smallest missing positive number in an array

Any suggestions to improve? ...
7
votes
1answer
2k views

Variable length integer encoding in Java

I have coded up the variable length integer encoding aka VLQ in Java. It works with any base in (1, 128]. I've implemented the usual encode/decode methods and also ...
4
votes
2answers
193 views

Number class to add/compare different kinds of numbers

I am creating a Number class having Integer and Fraction derive class which add/compare different kinds of numbers. In addition, the caller need not needs to know ...
2
votes
2answers
57 views

Dividing a long (arbitrary-precision) number by an integer

As part of a (slightly) larger program, I needed to write a function that could take an arbitrarily large number and divide it by a small number. In this program, I will be dividing this number ...
0
votes
2answers
888 views

Convert NSInteger into NSString

This is my sample code to convert a integer into string. Is it very costly to do the multiple loops of [NSMutableString stringWithFormat:..]? I.e should I just use appendString and get the NSString ...
5
votes
2answers
4k views

itoa base function in C

This is a function that converts an integer value to a null-terminated string using the specified base and stores the result in a char array that I must allocate (...
2
votes
1answer
2k views

Avoiding “warning: Use of uninitialized value” when fetching an integer CGI parameter

I was using this code to fetch a CGI parameter: $page = int(param('page')); This sometimes results in: ...
5
votes
1answer
2k views

Normalize integer types to float range

I wrote two template functions to take an integer type and output a float in the given range. One function takes signed integers and the other takes unsigned ...
3
votes
2answers
354 views

Merge and sorting arrays of String and int in more efficient methods

Are there better methods for merge and sorting arrays of int and String in more efficient methods both time-complexity wise and ...
3
votes
1answer
1k views

Handling GetTickCount() overflow in timeouts

This is code which must run on XP, so no GetTickCount64, and which should correctly handle the value wrapping around after 49 days. Can it be improved? ...
4
votes
1answer
162 views

LEB128 in Haskell

I have implemented the LEB128 encoding for unsigned integers. I'm fairly new to Haskell so I have some doubt about the coding style I'm using, particularly the definition of ...
11
votes
1answer
1k views

Portable safe unsigned integer arithmetic

tl;dr The purpose of the code is to allow writing ...
2
votes
2answers
479 views

Calculating the sum of the digits in 2^1000

I have a program which calculates the sum of the digits in the number 21000. The program is not beautiful and I was hoping for some advice on how to improve it. The idea I have used is to create an ...
2
votes
1answer
335 views

String-casting utility functions

I wrote a small header that is supposed to take care of calling the right strtoX or or stoX function for me and doing so at ...
5
votes
1answer
73 views

`read` re-implemented for Int's

I have written a safe read function for haskell (for education!). I think this is fairly idiomatic haskell code, but I'd like to know for sure. If you see anything else that could be improved, please ...
3
votes
2answers
268 views

Get number from N

I'm trying to find the shortest and best way to achieve the following: Given input integer $N, get the following output: n = 0, output = 0 n = 1, output = 0 n = 2, output = 10 n = 3, output = 100 ...
12
votes
1answer
951 views

Corrupting Java arithmetic through reflection

Inspired by a PPCG answer, I wrote this code, which shuffles the autoboxing caches of Number subclasses. This leads to, among other things, outputting ...
3
votes
1answer
578 views

Comparing a string that can hold a numeric value

I am refactoring a part of big old system developed in Java. I came across a situation where we are migrating some data types, and there will be use of both types. For compatibility, as all the old ...
6
votes
1answer
1k views

Large integer class for storing integer data as a char array

I am working on a LargeInteger class to store Integers beyond the size of long long as a dynamic character array. I have ...
2
votes
1answer
3k views

Simple calculation program in assembly MASM

I am a beginner in assembly. This program calculates: (Z+Z)+(X-Y) ...
0
votes
2answers
1k 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: ...
7
votes
2answers
1k 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 ...
2
votes
2answers
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 ...
5
votes
1answer
116 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 ...
4
votes
1answer
101 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 ...
3
votes
1answer
60 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: ...
12
votes
4answers
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 ...
4
votes
3answers
359 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 ...
11
votes
4answers
1k 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: ...
12
votes
6answers
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 ...
26
votes
4answers
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 ...
2
votes
0answers
540 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 ...
7
votes
1answer
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 ...
12
votes
2answers
3k 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 ...
8
votes
2answers
398 views

Implementing min of 3 numbers in Mic-1 code

I have the following algorithm to find a minimum of three input numbers: ...
6
votes
2answers
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 ...
8
votes
2answers
192 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 ...
12
votes
3answers
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 ...
2
votes
3answers
228 views

Long arithmetic addition in JS

The implementation of the algorithm which adds two number in string form with arithmetic rules. ...
5
votes
1answer
1k 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 ...
5
votes
1answer
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? ...
11
votes
5answers
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 ...
2
votes
1answer
153 views

Text representation for numbers with python

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

1 2 3
4
5
7