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
5
votes
2answers
481 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. ...
3
votes
2answers
122 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/...
4
votes
2answers
73 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 ...
8
votes
2answers
140 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 ...
11
votes
1answer
3k 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...
3
votes
2answers
352 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 (...
18
votes
7answers
17k 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 ...
7
votes
1answer
330 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 ...
4
votes
3answers
110 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 => ...
6
votes
3answers
3k 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 ...
3
votes
2answers
788 views

Assembly: Sum up the single bytes of a 32bit-register to a checksum

Exercise description: "Write a program that takes a double word (4 bytes) as an argument, and then adds all the 4 bytes. It returns the sum as output. Note that all the bytes are considered to be of ...
7
votes
0answers
135 views

Find an arithmetic expression near to target value

I wrote this program as an answer to Express a Number challenge on the Programming Puzzles & Code Golf site; it may be worth looking there for the context. The concept is simple (and familiar to ...
3
votes
1answer
121 views

Counting digits of numbers

Write a program that loops prompting for positive or zero integers of data type long. Then the number of digits the integer consists of (in decimal representation) should be printed to stdout. ...
1
vote
1answer
2k views

Forced and easy hton/ntoh conversion

Context: When sending message across the network, the sender has to convert the message from host byte order to network byte order and the receiver has to do the the reverse - network order to host ...
1
vote
0answers
158 views

Wrapper for GMP in C++

I'm writing a C++ high precision library based on GMP, sample code(files which have to be added to project): ...
3
votes
1answer
5k views

Assembly 8086 program to input a 16-bit signed number (in string form) and output its binary equivalent

I'm working on an exercise using Assembly 8086 which inputs a number (in string form) then output the binary form. Here is what I did (tested on emu8086 by the way): Convert the string to numeric ...
5
votes
2answers
340 views

Improved Van Emde Boas tree based map in Java

After working on Van Emde Boas tree -based map in Java, I came up with this code. I managed to fix the bugs and improve the performance by using internally primitive integers instead of wrapped ...
3
votes
3answers
3k views

Assembly x8086 (emu8086) - Display 32bits number on screen

this is my code (assembly x8086, not MIPS, and I'm using emu8086) to display a 32-bits number on screen. Of course the basic algorithm is as follows: ...
5
votes
2answers
4k views

Adding two Integers in an Array

I was wondering if I could get a second look at my program before I turn it in. The job was to code a program in java that adds two integers to an array gather the sum while displaying the integers ...
5
votes
2answers
3k views

Implementing String to int (atoi) in Java

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus ...
3
votes
1answer
666 views

Integer to binary using an old-school way

I know I could achieve this using System.out.println(Integer.toBinaryString(num)); in Java. However, I wanted to do it this way. ...
13
votes
2answers
2k views

Create the English word for a number

I cobbled together a function from various sources that takes an integer and returns it as its respective English word formatted as a string. The function is as follows: ...
1
vote
0answers
212 views

Cookie Clicker esque game idea

I am making a cookie clicker type game as a small project, within Xcode, written in Swift. I have a been developing a function which will turn a Int64() into a 4 ...
5
votes
2answers
3k views

Hamming distance between numbers in JavaScript

In Leetcode it states that my runtime is only faster than 38% all of submitted JavaScript solutions. Is there anything I can change to make it more efficient? ...
0
votes
1answer
182 views

Scala implicit conversion to add methods to Int

I started learning Scala, and like most functional languages, things get messy pretty quickly for a beginner. Here I have built a wrapper class for Ints so that I ...
5
votes
1answer
86 views

Format an Unsigned Byte to a Three ASCII Digits

I'm working on a game for the Atari VCS and have been dusting off my 30+ year old skills in writing MOS 6502 assembly language. The VCS runs at 1.19 MHz and is based on a MOS 6507 chip with some ...
-5
votes
2answers
680 views

Finding the largest prime factor of a 12 digit number [closed]

I created this using C++ and this gives the largest prime factor of a number.But my problem is if I try to find the largest prime factor of a very large number (12 digit number) it gives an error or ...
11
votes
4answers
10k views

Convert a boxed integer to an nullable integer with potentially different type

Given a boxed integer (with a boxed short, int, ...) I want to convert it to an unboxed, nullable type instead. It should be ...
1
vote
0answers
56 views

Numeric parser for integers, big integers, and doubles

I have written the following numeric parser. It may not be as fast as Boost's lexical_cast, nor as convenient, but it does get the job done and it does look good. I'...
11
votes
3answers
10k views

Endianness conversion in C

I have written a simple C header for converting the endianness of short integers and long integers. It uses the GCC macro ...
4
votes
3answers
4k views

Convert a numeric string to number but without python's built in function

I'd like to get a code review and some insights of whether I chose a good way to do this. What would other, clever ways look like. ...
11
votes
3answers
9k views

Implementation of C Standard Library Function ntohl()

This is an implementation of ntohl() that I wrote as an exercise. ntohl() takes a uint32_t ...
4
votes
1answer
72 views

From a string of 3 numbers to 3 integers

I'm solving some puzzles to learn Rust. A problem I faced is to turn a string of 3 integers into 3 integers. I succeeded with the following example: ...
6
votes
1answer
55 views

Storing angles with overflow errors

Rather than checking if my angle is in the range of 0 to 2pi every time it gets set, I got the idea to store it as an unsigned short with 0xFFFF being +2pi, thus the standard overflow behavior for ...
5
votes
1answer
110 views

Enforcing correct input/output of integers

The problem: Some types, like cstdint aliases or, at least purely theoretically, even std::size_t, may be (but don’t have to be)...
10
votes
3answers
599 views

Counting the number of “on” bits in an int

Here is some code I wrote as a solution to Programming Exercise 15.3 in Stephen Prata's C Primer Plus, 6th Edition. It is probably worth pointing out that the title of the chapter is Bit Fiddling. The ...
2
votes
1answer
397 views

Number representation for a calculator that works with very big numbers

I'm a beginner: I have learned concepts and features which I think I need in my project, but this is my first serious program and it is my internship project. I'm totally confused and need help to ...
2
votes
1answer
141 views

Maximum product of 3 integers in an int array using Python - follow up

This is a follow up to Maximum product of 3 integers in an int array using Python Changes to the code include renamed variables for improved readability and I added more test cases. ...
3
votes
5answers
834 views

Maximum product of 3 integers in an int array using Python

This is an interview question from Interview Cake: Given a list_of_ints, find the highest_product you can get from three of the integers. The input list_of_ints will always have at least three ...
-2
votes
1answer
671 views

Construct: VarInt integer encoding

This code is part of the Construct library. Docstrings explain what the code is supposed to do. ...
1
vote
1answer
128 views

Using strtol() follow up

Link to original question: Successful use of strtol() in C After getting excellent feedback from the code review community I attempted to redo my string to long conversion program! I believe I ...
2
votes
1answer
112 views

Program to determine ranges of char, short, int and long variables, both signed and unsigned

I wrote code to determinate ranges of char,short int and long variables,both signed and unsigned.Please help me to improve my code :) Here is the code: ...
10
votes
3answers
3k views

Successful use of strtol() in C

Attempted to use strtol() to parse a string into an integer and to output any appropriate error messages for failed conversions. How did I do? How could/should it ...
6
votes
1answer
33k views

Converting a bytearray into an integer

I am working on a program to decode MMS PDU files. They are binary files and I am reading them byte by byte and decoding each header value as I come to it. One header value is the Date, and it's ...
8
votes
1answer
4k views

Integer-to-ASCII algorithm (x86 assembly)

This is my best effort at converting a 32 bit integer in EAX, to an 8 character ascii string (result in RDI). It will work ...
4
votes
3answers
323 views

Dividing Negative integer ranges

Context: My project has a hand-rolled ORM (... yeah ... I know) ... built around the MS DataSet objects that represent wrapped collections of rows of DBtables (... yeah ... I know) ... and which ...
3
votes
2answers
438 views

Converting bits to decimal (integer) string is so slow

I am implementing myself a BigInteger class. It can natively handle very very big digits in very short time. ...
1
vote
2answers
105 views

Finding shortest sequences of integers whose product is larger than n

Here is the task: For an integer n, return a list of the k length integers sequences whose product is larger than n but as close to n as possible (i.e. smallest larger than n number), and whose ...
3
votes
1answer
2k views

Unsigned integer division ARM Cortex-M0+ Assembly

I am writing a subroutine for unsigned integer division in Assembly. I will call the subroutine DIVU. Inputs: R1 will be the ...
1
vote
1answer
90 views

Translating binary data from a file into a very large base-ten integer

I'm making an experimental Java algorithm that translates binary data from a file into a (very large) base ten integer. I am using BigInteger since this number may ...

1 2
3
4 5
7