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
-2
votes
1answer
71 views

Find the first palindrome larger than the given number

Question Input number of test cases. Input each test case and output its next palindrome number. Link: https://www.spoj.com/problems/PALIN/ Language Used: C++14 (gcc 6.3) My code ...
3
votes
2answers
248 views

Reading lines of integers into a list of lists

I have a text file containing integers in different lines which I want to read into a list of lists. So 01 11 14 45 50 09 should become ...
2
votes
1answer
125 views

Printing 64-bit decimal numbers in freestanding environment

I need to print signed 64-bit numbers in decimal form. Program runs in freestanding environment (no C library available, libgcc may be unavailable too, or may not work correctly). So I can't use ...
6
votes
3answers
198 views

Custom integer parser optimized for performance

I want an integer parser so fast it risks discovering time travel. I'm stuck in C# for a language, but that doesn't mean I can't try to use c++. So I wrote the following: The best I could do ...
3
votes
3answers
120 views

Safely & portably read a value into a predefined integral type of varying size like mode_t

I need to read a value from a text file and put it into a predefined type, in this particular case mode_t. This typedef can have a variety of types underlying it, ...
8
votes
4answers
2k views

reinterpret_cast vs bit shifts for extracting 32-bit integers from bytes, with either endianness [closed]

I've written some simple helper functions that read data types from a vector of bytes (binary files) and having a tough time deciding which route to go. Method 1 : reinterpret_cast ...
11
votes
2answers
2k views

Computing the integer square root of large numbers

The "integer square root" of a non-negative integer \$ n \$ is defined as the largest integer not greater than \$ \sqrt{n} \$: $$ \operatorname{isqrt}(n) = \lfloor \sqrt{n} \rfloor = \max \{ k \in \...
12
votes
1answer
7k views

Thread-Safe Integer Sequence

I would like to give an Integer to whatever demands it. For that I use the following code ...
6
votes
2answers
198 views

(Almost) arbitrary base ints with decorated methods

After reading this question about returning the "next" string in some alphabet (the digits, followed by upper and lowercase ASCII letters) using Java, I thought, well that's just counting, so why not ...
6
votes
1answer
640 views

Haskell BigInt addition

I have a function add that adds two lists of integers: add [1, 2] [4, 5, 8] == [4, 7, 0] add [9, 8, 9] [1, 3] == [1, 0, 0, 2] ...
2
votes
1answer
57 views

Check whether an integer's leftmost bits are ones in Rust

I'm trying to validate if an integer represents a valid IPv6 mask, which boils down to checking if all the left-most bits of an u128 are 1s, and all the right-most bits are 0s. For instance: ...
8
votes
3answers
704 views

'StrictInt' Python object class that prohibits casting numbers with non-integer components to `int`

Inspired by and created because of python3 utility: convert safely to int and driven partly by the 'refactor' in my answer. The aforementioned question and my answer drove me to go and do a thing ...
3
votes
1answer
97 views

Detecting over-/underflow for uint64+int64

I am trying to write a function in Go which adds an uint64 value and an int64 value and which detects whether the result can be ...
6
votes
1answer
855 views

Convert unsigned Integer to ASCII in x86 assembly

This is my best effort to code an x86 32 bits to get an unsigned int (4 bytes) and convert it into a string. My strategy was successively divide the number by ten and fulfill the remainder of the <...
23
votes
4answers
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 ...
2
votes
0answers
495 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 ...
2
votes
1answer
6k views

Tough List Comprehension Split Letters and Numbers

So, another challenge from my professor. I've finished it, but wanted to get some feedback, and see how other people might work this problem. Assume you have a string, such as "Hello 34215 World ...
15
votes
2answers
5k views

Big integer class in C++

I have been trying my hand a bit at creating a big integer class in C++. This is a continuation of a homework assignment. The class stores a big integer in a double linked list. Each slot contains 8 ...
13
votes
0answers
165 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. ...
4
votes
3answers
181 views

Caching an integer - distinguishing no value from zero

I would like to store an integer value in the application cache. The GetOrSet method should always return an int, and never be null. If there is no cached value, ...
3
votes
0answers
536 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 ...
12
votes
5answers
14k views
5
votes
0answers
63 views

C++ library with tools for (arithmetic) type manipulation and mixing

Broken out from a previous question (C++17 saturating integer (arithmetic) type library) is a small C++17 header-only library to deal with issues that arise working with arithmetic where signed, ...
10
votes
2answers
1k views

C++17 saturating integer (arithmetic) type library

I wrote a C++ library to provide extra signed and unsigned integer types that saturate in overflow situations. It's in proof of concept stage and I'd love to get some feedback on it. A short usage ...
10
votes
5answers
17k views
5
votes
1answer
83 views

Printing limits of Integral Types in C

I intend to demonstrate the proper way to use format specifiers for different data types and also display the respective limits. This is meant to be an example. ...
1
vote
2answers
2k views

Reversing the bits of a 32-bit integer

Here is my code: ...
2
votes
1answer
5k views

MIPS assembly string to int function

I have written the following code for a programming assignment. I would appreciate some help to improve this code and see if it meets all the requirements and criteria. I would like to improve my loop ...
7
votes
2answers
410 views

A program which adds 2 integers in the range of 0 to 10^100

I wrote this program to help myself learn C. It is the result of a few days of hacking things together until it worked. This program takes 2 integers as inputs in the range of 0 to 10100 and outputs ...
6
votes
2answers
493 views

Variable integer encoding

There is a varint encoding, which I call LEP64 (Little Endian Prefix Max 64-bit). Basically, the first byte of the encoded value tells us, how many bytes follow (needed bytes encoded in unary coding, ...
5
votes
2answers
264 views

Improved Van Emde Boas tree based map in Java

After working on Van Emde Boas tree -based map in Java, I came up with this code. I managed to fix the bugs and improve the performance by using internally primitive integers instead of wrapped ...
5
votes
3answers
4k views

Decimal/binary/hex converter

I've finally finished my converter and have determined that it works, but I'm trying to make it cleaner and/or more practical (primarily with the switch statements)....
4
votes
3answers
766 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:...
1
vote
1answer
1k views

Forced and easy hton/ntoh conversion

Context: When sending message across the network, the sender has to convert the message from host byte order to network byte order and the receiver has to do the the reverse - network order to host ...
4
votes
1answer
172 views

Non-overflowing avg calculation with integer arithmetic

I have multiple long values and I want to calculate their average value using integer arithmetic, without precision loss and using java rounding rules for division, ...
9
votes
4answers
6k views

Format an integer with thousands separator (recursive implementation)

The function accepts a number n and displays it recursively with commas. As well as a general code review, I'm interested in suggestions for alternatives to ...
4
votes
3answers
388 views

Parse a stream of space separated numbers

I need to parse a stream of characters IEnumerable<char> as a list of space (and newline) separated numbers. Numbers can be expressed in base 10, base 8 (...
4
votes
1answer
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 ...
5
votes
5answers
3k views

Swapping two integer numbers with no temporary variable

I tried to swap 2 integer numbers without using an additional variable as a traditional swap. Is it legal in C++? My VC compiler doesn't complain nor gives any warning about it. If so, how can I ...
23
votes
4answers
51k views

Simpler 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 tests)? Assume 2's complement ...
0
votes
3answers
177 views

Divisions using Arraylists

Exercise: Using Arraylists create a program that makes divisions between two very large numbers (numbers greater than 10 digits). I did it (the code works), but could my code be improved? ...
27
votes
10answers
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? ...
1
vote
2answers
715 views

String of numbers to Integers (without using .to_i)

My exercise was to take a string of numbers, such as "1234" and return them as an integer 1234. However, I wasn't allowed to use #to_i, ...
5
votes
2answers
2k views

Hamming distance between numbers in JavaScript

In Leetcode it states that my runtime is only faster than 38% all of submitted JavaScript solutions. Is there anything I can change to make it more efficient? ...
5
votes
3answers
680 views

BigInteger class, supporting addition only

Here is just addition, no multiplication or subtraction, but I will implement them later. Before going on, I'm here to ask for advice about the design. ...
7
votes
5answers
451 views

Sum a list of string numbers

I have a list of 32-bit signed numbers in range [-10e5, 10e5] in string form read from stdin, and I would like to compute and print their sum in the fastest way possible. The bottleneck is in the ...
3
votes
1answer
2k views

Validation of integer inputs from a BufferedReader

At my school, we are using the BufferedReader for keyboard input for our console apps and I don't want to have to write ways to validate input for every project so I am writing a class to validate ...
10
votes
1answer
6k views

Multiplying and adding big numbers represented with strings

I got a very unusual problem of adding and multiplying very big numbers (≥ 1e+100). So I've written simple functions that would operate on string representations of numbers, both as an input and an ...
6
votes
2answers
404 views

Shifting an 128-bit integer consisting of four 32-bit integers

For a little project of mine I have written two versions for shifting a 128-bit unsigned integer consisting of four 32-bit unsigned integers in x86 assembly. I cannot really decide which is better in ...