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
0answers
38 views

Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
3
votes
1answer
56 views

Python maintain precision for currency

Working with currencies I am aware it is not good to work with floating point values due to rounding and maintaining precision. Better is to work with integers. Would the following Currency class make ...
2
votes
2answers
59 views

Iterating over a range of integer ranges in Java

My use case is this: RangeDemo.java: ...
3
votes
2answers
104 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
29 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
119 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
598 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
89 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
135 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
153 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
168 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
100 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
37 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
139 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
187 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
185 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
410 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
257 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
244 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
118 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
57 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
93 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
111 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
74 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
136 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
103 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
123 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
162 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
4k 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
70 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
122 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
3k 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
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 ...
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 ...
2
votes
1answer
55 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
703 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
638 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
86 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
836 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
160 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. ...