Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

2
votes
2answers
57 views

Iterating over a range of integer ranges in Java

My use case is this: RangeDemo.java: ...
3
votes
2answers
97 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 ...
2
votes
1answer
26 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 ...
5
votes
2answers
116 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 ...
13
votes
1answer
571 views

128-bit unsigned integer

This implements 128-bit unsigned integer using C++14. It works on MSVC and 32-bit architectures being complementary to the unsigned __int128 type provided by GCC ...
4
votes
1answer
264 views

Converting long values in Java to unsigned strings

This one is a research attempt to find out how to convert long values as unsigned long integers to Strings. It is much slower ...
7
votes
1answer
84 views

Calculating the cube root

Inspired by a question on SO I was checking how to calculate the cube root of an integer and found a C-ish solution on Hacker's Delight: ...
7
votes
2answers
121 views

Register “%b” conversion specifier

I'm writing a library to register the b, B conversion specifiers and make them work the closest possible to ...
4
votes
1answer
50 views

Program containing several bitset based classes including chars, strings, ints and a base bitset class

This is by far the biggest project i've ever made. The main goal was just to make a bitset class but it expanded into making several classes based off of that main bitset class. Some things I would ...
2
votes
3answers
152 views

Armstrong number generator

This is my code to generate all possible Armstrong numbers between the two given numbers. The logic uses string instead of integer to separate the digits to optimize the code. ...
-2
votes
2answers
164 views

ArmstrongNumber checker

This is my code written to check whether a given number is armstrong or not . but the logic differs from the conventional logic of using integer and separating its digits and calculating , instead i ...
4
votes
2answers
99 views

Defining a class that behaves exactly like an `int` [closed]

This is an exercise to create a class that behaves exactly like an int. Questions Am I missing anything? Or did I going about this about this the wrong way? Is ...
2
votes
1answer
35 views

Determine if a String can be parsed to LocalDateTime or Int

I have the following kotlin function that takes in a string "value", then attempts to parse "value" as both a LocalDateTime and an Int. It returns a Pair of the parsed value of "value" and an enum ...
3
votes
1answer
121 views

Java BigInteger implementation

I've written a java application that can handle operations on big integers implemented with array list of digits. I have a difficulty with the way I implemented the divide method, because when the ...
3
votes
2answers
182 views

Reverse int within the 32-bit signed integer range: \$[−2^{31}, 2^{31} − 1]\$ Optimized

LeetCode Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Feedback Optimized from beta code in the original question here. Based on this LeetCode ...
4
votes
1answer
182 views

Reverse int within the 32-bit signed integer range

Problem Reverse digits of a 32-bit signed integer. When the reversed integer overflows return 0. Optimized code here. Feedback I'm looking for any ways I can optimize this with modern C++ ...
-3
votes
1answer
58 views

Code creates huge Text File [closed]

A Java program that writes each digit on a new line, from 1 to max integer. We should end up with 2.4 Billion lines. After looping to near the maximum integer, the text file becomes really huge. Like ...
8
votes
1answer
409 views

Prime factors and perfect square

I want to convert an integer to a perfect square by multiplying it by some number. That number is the product of all the prime factors of the number which not appear an even number of times. Example ...
3
votes
2answers
254 views

Adding thousand separator to various number types

Am making a pure .net library with helper functions (GitHub). However I wanted to have a thousand separator for all number types and here is what I am currently doing ...
6
votes
3answers
243 views

Encoding 0/1 bits to 0/255 bytes in C++

I need to map a sequence of bits that represents a float into a sequence of bytes by this rule: the value 1 maps to the value 255 and the value 0 maps to the value 0. This is the code I have now: <...
3
votes
2answers
110 views

Change Arithmetic Right Shift to Logical Right Shift

The following code is a solution to a textbook (Bryant&O'Hallaron: Computer Systems A programmer's Perspective 2nd Ed) problem in bit-level data manipulation (attempted for the challenge, not a ...
0
votes
1answer
55 views

Breaking a 32-bit value into bytes for an ArrayList

I am very new to Java and its collections and I'm trying to figure out "best" way to populate an ArrayList<Byte> in Java. In particular, I'm trying to take a ...
2
votes
2answers
92 views

BigInteger check in C++ from a string

You might consider this question a follow-up of: BigInteger check in C from a string Although, this time, I used C++. I am on Linux Mint 19.1 with the compiler version: ...
4
votes
2answers
85 views

BigInteger check in C from a string

I have started to learn C. At the time of writing I don't know anything about the language. The following I wrote today in a few hours. Though I do have a lot of programming experience, I find C hard ...
0
votes
1answer
54 views

Calculating the volume of a box

I've been studying more C using C Programming: A Modern Approach: A Modern Approach 2nd Edition by K.N. King as well as reading the comp.lang.c FAQ and the Code Review section here on Stack Exchange ...
1
vote
3answers
109 views

Functions to handle big numbers in C

I'm working to implement some simple bigint functions for addition, multiplication and powers. They work correctly but the larger the numbers the longer the time it takes. For example 210000 takes a ...
0
votes
2answers
67 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 ...
1
vote
1answer
130 views

Addition function for two Big Integers represented as std::string

This function is from my attempt to write a Big Integer class in C++. Below, I have a simple addition function. It takes two strings of arbitrary length which contains only digits (no '-' sign as ...
4
votes
2answers
101 views

Test whether a number is a Harshad Number

A little code I wrote that will check if the entered 4-digit number is Harshad or not (Harshad number is one which divided by the sum of its digits will leave no remainder, special Harshad is the same ...
3
votes
2answers
68 views

Expressing positive integers as the sum of different charming numbers

In a practice academic interview of mine, we discussed question six, round one, of the United Kingdom Mathematics Trust's 2015 British Mathematical Olympiad. Which states: A positive integer is ...
5
votes
1answer
116 views

C++ big unsigned integer class

Here is a big unsigned integer type I intend to use for learning and "fun" with cryptography. It uses a std::vector for storing blocks of unsigned integers, the ...
5
votes
1answer
143 views

AVX512 - Add two 512-bit integers using _addcarryx_u64 function

My purpose is to add two __m512i variables (c = a + b) as efficiently as possible. To do so, I'd like to use the _addcarryx_u64 ...
5
votes
3answers
3k views

An exception-safe wrapper for std::stoi

std::stoi may throw exceptions so it needs to be surrounded by try/catch. In applications where std::stoi may be used ...
0
votes
1answer
99 views

Hash function in C for sets and associative arrays

I wrote a non-cryptographic hash function in C, which I am hoping might be suitable for use in an implementation of hash sets or hash maps. ...
1
vote
2answers
34 views

Extract and divide algorithm implementation in Elixir

I just started learning Elixir and stumbled upon this challenge over on Programming Puzzles & Code Golf. It is a well-suited task for beginners, so I chose to give it a go (to be clear, give it a ...
-2
votes
1answer
68 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 ...
2
votes
1answer
121 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 ...
9
votes
2answers
2k views

Int to Enum Extension Method

As you know, converting an int to an enum is straightforward. Doing it generically with type/range checking isn't. I looked for a reusable example since this seems like a common situation but did not ...
6
votes
3answers
196 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
119 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 ...
6
votes
2answers
197 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 ...
2
votes
1answer
52 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
701 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 ...
6
votes
1answer
637 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] ...
3
votes
1answer
82 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
819 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 <...
13
votes
0answers
157 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. ...
3
votes
0answers
511 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 ...
4
votes
3answers
164 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, ...