Tagged Questions

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.

learn more… | top users | synonyms

5
votes
2answers
59 views

Finding the Greatest Integer and Ties

I'm trying to find the greatest integer of four integers and its ties. I used the following code. Is there anyway to simplify this more effectively? ...
10
votes
3answers
539 views

Beginnings of a base-conversion game in Python

I'm pretty very new to python, so I thought that I'd write a game to teach me python and better teach me how to think in hexadecimal and binary. I'm starting off by writing a few functions to convert ...
8
votes
2answers
190 views

Parsing integers safely

Background I'm writing user input validation for a client-side JavaScript web application. Users enter numerals representing an integer into an <input>. As ...
7
votes
1answer
95 views

Lossy packing 32 bit integer to 16 bit

I am working on the lossy 16 bit representation for 32 bit integers that catches all 32 bit range with precision depending on absolute value. My idea is to store integer ...
5
votes
4answers
170 views

C library that converts integers to string and vice-versa

I've created this little library to help ease myself (and for others, hopefully) with the pain of having to convert integers to string and vice-versa. It's written in C, and for maximum portability I ...
2
votes
1answer
86 views

Encoding and decoding exercise

I was issued the following coding challenge from a company to write an encoding and decoding function: Encoding: This function needs to accept a signed integer in the 14-bit range [-8192..+8191] ...
3
votes
2answers
89 views

Emulating __uint128_t

I developed a lot of code locally with __uint128_t only to find out that it is not available on the target platform. I am now trying to write my own struct that ...
0
votes
1answer
75 views

Bignum library with arbitrary bases

I just started development on a new bignum library that can operate on numbers of any arbitrary base (up to 232). I want to get a good idea of where I can improve before taking things any further. ...
8
votes
3answers
721 views

More elegant way to increment Integers in Map?

I have a HashMap<Token, Integer>, which counts occurrences of Token. Each time Token ...
5
votes
2answers
168 views

Why are these functions slower than BigInteger's included methods?

I tried writing alternative functions to Java's BigInteger.add(BigInteger.ONE), BigInteger.add(BigInteger), and ...
2
votes
1answer
115 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 ...
10
votes
1answer
297 views

Thread-Safe Integer Sequence

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

Function for splitting an integer into smaller values

This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way? ...
5
votes
1answer
40 views

Converting the integers to their equivalent string representations

I have the following function which converts an integer (such as 103) to its string representation ("one hundred three"): ...
2
votes
2answers
181 views

Two solutions with min max values - which should be slower?

While practicing at codinbat.com on the assignment: Given an array length 1 or more of ints, return the difference between the largest and smallest values in ...
2
votes
1answer
108 views

Write int, short int or any integer value in a file in binary form using fputc

I am trying to write binary values in a file using fputc. In my case it is a short int. The file must be opened in "w" mode. ...
4
votes
3answers
2k views

Converting an integer to binary, hex, and octal

I've started caring about standards and the beauty of my code. I doubt whether I've written it correctly and aesthetically. Can you please judge this simple, exemplary program? I want to know of any ...
3
votes
2answers
52 views

Rounding ints division result to certain “granularity”

In my program I call "granularity" the "minimum step" for a number. For example, if granularity == 4 then I only work with numbers "0, 4, 8, 12 etc" (so normal ...
11
votes
2answers
512 views

Division without / operator

I created a method that does division without the / operator. I believe this code is a bit slow and can be improved, but I am not sure how to do so. I'm not so good ...
4
votes
2answers
160 views

fractions data type — when to reduce?

Here is my Python implementation of a simple fractions class: ...
8
votes
2answers
114 views

Container for list of ranges

I made a C++ container of an ordered list of integers, that, in order to save space, I save as ranges: For example, the list \$ \{1,2,3,5,6,7,8,9,20\} \$ is saved in memory as \$ \{(1,3), (5,9), ...
7
votes
3answers
233 views

Optimizing unique partitions of integers

I am working on a project involving the unique partitioning of integers. I.e. I am looking to have all of the partitions of n = x1 + x2 + x3 + ... + xk, where xi = xj implies i = j. For example, if I ...
4
votes
2answers
572 views

Checking endianness at compile-time

I've tried to make a portable way of ensuring endian-specific code gets generated at compile time using C++11, however I only have a computer with Windows on it to test at the moment. Because of this, ...
7
votes
2answers
398 views

Int stack implementation

...
14
votes
4answers
4k views

Displaying each number of an integer in a sequence

One of the books I'm currently reading to learn C# asked me to write a method that takes an integer between 1 and 99999 and displays each number in that integer in a sequence, separating each digit by ...
5
votes
1answer
54 views

Conversion from decimal to negabinary and vice versa in Haskell

Recently I set about to writing conversion functions in Haskell between decimal and negabinary. I tried to write them in as much functional a style as possible, also aiming at brevity. Below I present ...
5
votes
2answers
93 views

What might be another way to test if int is 32 bits wide at compile time?

Below is code that will compile when INT_MAX is equal to 232. It will generate a compiler error when INT_MAX is not equal to ...
5
votes
3answers
332 views

Number to words problem

This is a very long solution I've been working on to a Number to Words problem. I've identified that there's a lot of repeating logic (e.g. 'if writing > 0', 'writing = integer /',). How would you ...
2
votes
1answer
78 views

Check robustness of function to add an interval to a date

Background of the problem My Class level variables: ...
13
votes
4answers
3k views

Counting number of 1's and 0's from integer with bitwise operation

Is there a better way of doing this? ...
5
votes
2answers
2k views

Simpler method to detect int overflow

To detect int overflow/underflow in C, I use the below code. What might be simpler and portable code? (that is: fewer tests) Assume 2's complement and don't ...
3
votes
1answer
2k views

Writing integer to binary file

I wrote a python function that writes a integer to a file in binary form. But are there any ways that I can reduce the number of steps, is there a more efficient way to write a integer to a file? ...
7
votes
1answer
6k views

How to use ByteBuffer properly?

I am trying to use ByteBuffer properly with BigEndian byte order format. I have couple of fields which I am trying to put together into a single ByteBuffer before storing it in Cassandra database. ...
3
votes
1answer
1k views

Breaking down a dollar amount into bill and coin denominations

I've started coding C++ again after a 6 month break to prepare for a class I'm taking this quarter. I found this idea and thought it would be fun to try and code. I originally started with input as a ...
5
votes
3answers
2k views

Split a long integer into eight 4-bit values

It's an exercise from a book. There is a given long integer that I should convert into 8 4-bit values. The exercise is not very clear for me, but I think I understood the task correctly. I googled ...
4
votes
1answer
173 views

What do you think about this uint64 random generator?

I've looked for a good method to generate an integer inside a range of values. I think using modulo is not a good method because the values are not perfectly mapped in the range. So I mapped the ...
3
votes
2answers
297 views

Long-to-string and string-to-long while checking characters within a xlate table

I am attempting to come up with a better method for doing this: ...
4
votes
2answers
2k views

Separate numbers with commas

How should I approach refactoring this code? ...
4
votes
3answers
390 views

I'm wondering if I'm doing this whole Haskell thing right

There may not be a "right" way to do it, but I've come to perceive Haskell as the kind of language, where literacy of the proposed programming paradigm is crucial to writing proficient code in the ...
5
votes
2answers
727 views

Determine if an int is within range

Can I somehow make this a bit cleaner using Math.[Something], without making a method for it? ...
0
votes
2answers
1k views

Spotify's “Reversed Binary Numbers” Problem [closed]

I wrote some Java code for Spotify's Reversed Binary Numbers puzzle and I tested it on my machine and I am almost positive that it behaves as expected. However, when I send it to puzzle AT spotify.com ...
1
vote
1answer
103 views

How to improve this functional python trial division routine?

The following functional program factors an integer by trial division. I am not interested in improving the efficiency (but not in decreasing it either), I am interested how it can be made better or ...
2
votes
2answers
84 views

NSArray and NSNumber-int conversions

I have a textView which is displaying a string. I also have an array which keeps track of where every line begins, it stores the "start index" or NSRange.location for every line. However, when text ...
4
votes
2answers
2k views

Effectively convert little endian byte slice to int32

I have a data stream of bytes and I'd like to get a little endian encoded int32 from four bytes. Is there a better way than to do this like the following code? ...
2
votes
1answer
2k views

Get int array from database Cursor

I find this code quite ugly, but in fact, I can't find a finer way of achieving the goal. ...
5
votes
2answers
330 views

Improving a custom integer class 2.0

As sort of a follow up to my previous question from a year ago, what are some suggestions you can think of that will improve this code, whether is directly programming related or algorithm related? I ...
6
votes
2answers
306 views

Integer to String recursive method

I was experimenting with lists, sets, and finally maps when I spotted a pattern in my code to make it recursive. Now I haven't used recursion much in the past or at work and I was very excited to have ...
16
votes
3answers
2k views

Int overflow check in Java

I have a piece of code that takes a couple of integers and check if performing an addition on the inputs would result in an overflow. I was wondering if this code is SOLID: ...
5
votes
3answers
2k views

Converting a range of integers from a string to an IEnumerable

Goal Convert the following into an IEnumerable of integers accounting for the x-y ranges: ...