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.
7
votes
2answers
192 views
Int stack implementation
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int value;
int is_empty;
struct Stack *next;
};
int pop(struct Stack **stack) {
if ((*stack)->is_empty == 1) {
...
13
votes
4answers
565 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
36 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
87 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 232.
#include <iostream>
#include <climits>
namespace ...
4
votes
3answers
59 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
66 views
Check robustness of function to add an interval to a date
Background of the problem
My Class level variables:
static int secondsInterval = 100000000;
static long secondsSpecial=0;
static Date dateSecondsReg;
static Integer intValue;
I ...
13
votes
4answers
608 views
Counting number of 1's and 0's from integer with bitwise operation
Is there a better way of doing this?
// Definition: Count number of 1's and 0's from integer with bitwise operation
//
// 2^32 = 4,294,967,296
// unsigned int 32 bit
#include<stdio.h>
int ...
3
votes
1answer
206 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 use wider integers.
int a,b,sum;
sum ...
-3
votes
2answers
81 views
I need help with integer division in PHP [closed]
I had a bit of code in BASIC I needed to convert, but specifically this following line has me stumped atm:
NRSLATHOR=INT(LSLATVER/PITCHVER-.8)
In PHP I'm attempting to do an integer division but I ...
3
votes
1answer
419 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?
def ...
3
votes
1answer
571 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 ...
4
votes
1answer
155 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 ...
4
votes
3answers
366 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 ...
2
votes
2answers
287 views
Determine if an int is within range
Can I somehow make this a bit cleaner using Math.[Somthing], without making a method for it !?
int MaxSpeed = 50;
if (Speed.X > MaxSpeed)
Speed.X = MaxSpeed;
if (Speed.X < MaxSpeed * -1)
...
0
votes
2answers
804 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
73 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
60 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 ...
2
votes
1answer
1k views
Android - get int array from database Cursor
I find this code quite ugly, but in fact, I don't see any finest way to achieve the goal.
private static final String TABLE_NAME = "table_name";
private static final String[] allNeededColumns = ...
5
votes
2answers
263 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
241 views
What can be done to improve a recursive method?
So 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 ...
14
votes
3answers
1k 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:
public static boolean ...
3
votes
2answers
486 views
Improving a custom integer class
How can I improve upon this integer class that I wrote? I am/will be using this for some calculation intensive crypto algorithms, mainly POLY1305AES. I might even write RSA using this. I know that ...
5
votes
4answers
244 views
Adjusting integer based on multiple elements in int array
I'm working with the following code which adjusts an int based on multiple elements of an integer array. I am wondering if there's a cleaner, easier-to-read construct for this:
int Blue = 0;
int[] ...
4
votes
1answer
496 views
Speed optimising JavaScript code
I've written this following rather naïve branch-and-bound based IP solver. My question is whether there are any obvious JavaScript optimisations that could speed it up? I am not looking for ...