The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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.

317 questions
Filter by
Sorted by
Tagged with
121 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, ...
3k 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 ...
201 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 ...
64 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: ...
705 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 ...
653 views

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] ...
115 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 ...
991 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 <...
181 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. ...
587 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 ...
200 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, ...
64 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, ...
2k views

Reversing the bits of a 32-bit integer

Here is my code: ...
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. ...
281 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 ...
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 ...
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 ...
515 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, ...
2k 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 ...
891 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:...
208 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, ...
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 ...
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 \...
442 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 (...
209 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? ...
777 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, ...
700 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. ...
473 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 ...
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 ...
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 ...
18k views

Checking if a number is power of 2 or not

This is my solution in Java and I need your opinion: ...
432 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 ...
797 views

Beautiful Number Validator

Problem Adapted from this HackerRank problem. A numeric string, $s$ is beautiful if it can be split into a sequence of two or more positive integers, $a_1, a_2,...,a_n$, satisfying the ...
373 views

WGS84-coordinate encoded as Integers

I want to store WGS84-coordinates in two integers for latitude and longitude instead of floats to get a higher precision than with floats. Here's my code: ...
2k views

Simple BigInt for sum and difference

I am working on a school project where we need to add and subtract large integers in C++. I will also be adding multiplication and division in the next couple days. So far it appears like everything ...
62 views

Smallest difference of pair in String-array

Let's say I have the following array of Strings: String[] rows = new String[]{ "", "A", "B C C", "", "B D E", "E F G H" }; As well as the following ...
3k views

MIPS assembly program to read integer byte by byte

I try to learn assembly by writing small programs. This MIPS program is supposed to read an integer from the terminal and print it byte by byte in hexadecimal. I think it works but I didn't check ...
159 views

Abelian sandpile

I am trying to programm the Abelian sandpile model in VisualBasic. Also, here is a youtube video made by Numberphile about the very same topic. Here is a picture ($2^{20}$ grains of sand) I made ...
378 views

Formatting big integers as hexadecimal digits separated by colons

I'm pretty sure this can be written as a couple of lines, but I'm unable to come up with a better solution. ...
106 views

Efficient integer input / output (with a use case)

The task: A set of functions used to read/write integers from/to stdin/stdout as efficiently as possible. Rationale: printf/...
71 views

F'up: Arbitrary large unsigned integers

This is a followup question to Arbitrary large unsigned integers. The feedback was very helpful and made me redesign large portions of the code. I changed the ...
136 views

Arbitrary large unsigned integers

Edit: There's now a Follow-up question concerning more recent code. From time to time, you see a question over at Stack Overflow about calculating factorials, typically asking why the result soon ...
2k views

Simple uint128_t implementation

I made a simple uint128_t implementation for a project that I'm working on. The reason for not using for example boost::uint128_t...
304 views

Solution to itoa

As some of you might know I've been working my way through the C Programming Language (2nd). I recently finished a rough implementation of the itoa exercise (...
15k views

Checking if each char in a string is a decimal digit

I had to write a program that would verify that a string contains any digits beside numbers 0-9 and the first '-' sign to indicate if pos/neg. The algorithm works like I want it to, but is there a ...
297 views

Convert size_t to big endian on Linux

I see that on Linux there are various built-in functions to convert endianness for specific integer width types, and they are probably faster than anything one can write and compile on their own. But ...
109 views

Count number of bits to convert one integer to another integer

Return the number of bits (N) that will need to be changed in order to convert an integer(X), into another integer(Y). Constraint: 0 <= X <= Y <= 2**128 Example: X = 1, Y = 2 => ...
2k views

User input validation that might be unbreakable for C++

I am new to programming and have been having a lot of trouble with user input validation. Everything I found online I could break and I finally put this function together that appears to be ...