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
9 votes
3 answers
1k views

C function to read only numeric values

I'm learning C and as exercise I'm trying to implement a custom function to parse a input from user and return successfully only if the input is a number. These are my files, how could I improve it? ...
shark_sh's user avatar
5 votes
1 answer
275 views

Identify distance degeneracies on square and hexagonal lattices

I have an interest in how the number of ways you can reach from the origin to a certain distance on both square and hexagonal lattices. If L is the square of the distance and integer pairs (i, j) are ...
uhoh's user avatar
  • 451
1 vote
1 answer
107 views

Calculate the cost of Biryani lessons

Problem: According to a recent survey, Biryani is the most ordered food. Chef wants to learn how to make world-class Biryani from a MasterChef. The chef will be required to attend the MasterChef's ...
kvk30's user avatar
  • 113
5 votes
1 answer
131 views

uint24_t implementation ported to Windows

I coded stuff for an embedded device where int is 24bits. I wanted to port uint24_t and ...
Vortex 2728182818's user avatar
2 votes
1 answer
105 views

Any amount bits integer

This class is meant to work exactly as an unsigned integer should work, but it limits the value based on a set amount of bits. ...
RhinoRunner's user avatar
6 votes
2 answers
142 views

C++ Arbitrary Precision Whole Number Class

The class below implements an arbitrarily sized whole number C++ class. It is wrapped around a bitvector class, which does most the binary heavy lifting. The basic ...
StormCrow's user avatar
  • 369
0 votes
1 answer
31 views

Mangle words to bytearray of lo/hibytes

I would like to refactor the following code snippet adapted from a project I'm involved. It is part of a code to control power outlets via network. The code snippet was written by myself based on the ...
Looser User's user avatar
2 votes
1 answer
84 views

Class for packing two 4 bit unsigned integer in uint8_t

The purpose for this class is to pack two 4 bit unsigned integer into 8 bit unsigned integer, e.g. uint8_t. It is made in a way, so the class to resemble array of ...
Nick's user avatar
  • 1,480
6 votes
2 answers
135 views

First attempt at making a sobel edge detection filter in c

I made a borders filter in C. It works properly, but I think it could be optimized and better designed; the formatting is abysmal. It uses too many lines for the border cases and has too many ...
GBA's user avatar
  • 63
3 votes
2 answers
187 views

Print decimal integer

This is an x86-64 Linux program to print a non-negative decimal integer. I would appreciate any simple optimizations for size and readability. I am aware that division by a constant is usually done by ...
qwr's user avatar
  • 1,016
0 votes
3 answers
145 views

Simple stack of integers

I've a simple push/pop implementation in my program: ...
3rdgrade-dropout's user avatar
5 votes
2 answers
2k views

128-bit integer type in GCC/Clang

When working on my projects, I often need a 128-bit integer type. For this purpose, I use the __int128 compiler extension. Here is the implementation I am currently ...
DaBler's user avatar
  • 299
3 votes
1 answer
249 views

Generic Integer Square Root

I have implemented an integer square root function that is branch-free and runs in constant time, using the first variant found in this answer as a base. All possible values for the types ...
Kittoes0124's user avatar
  • 1,940
1 vote
2 answers
328 views

Mapping bunch of integers to some strings with less repetition [closed]

I have this switch case statement which maps an integer to a string. There can be multiple integers that maps to same string. objective here is to not to duplicate the string value and retrive the ...
Pahan's user avatar
  • 21
8 votes
4 answers
3k views

Standard C overflow-safe arithmetic functions

If this code is written correctly, it will block undefined behavior from happening in the case of signed overflow (guaranteeing that the program halts instead), and also guarantee that the program ...
Thisis Afal Sename's user avatar
3 votes
1 answer
261 views

Portable integer to/from little endian conversion in C

Integers need to be converted to a byte array of defined endianness to be reliably and consistently saved and transmitted, and converted back to be accurately received and read. The goal is to be as ...
user16217248's user avatar
4 votes
1 answer
566 views

Python function: user input to get positive integer using assert

Is it a bad thing to use asserts to validate user inputs? Or should I keep using an if statement in case the number isnt positive? Code with assert statement: ...
eddyxide's user avatar
  • 141
2 votes
1 answer
92 views

Absolute value meta programming

I am implementing a generic absolute value function that handles signed and unsigned integer values correctly over the input type's domain. The std::abs(...) ...
dvnlo's user avatar
  • 145
6 votes
4 answers
2k views

Find all ways to express each of a list of even numbers as the sum of 2 positive, even numbers

This code was a challenge from a friend. Essentially she wanted a way to "Divide an even number into 2 even numbers." So I made this quick and dirty Python program to find the solutions up ...
Omar Morales Rivera's user avatar
1 vote
1 answer
54 views

SortedIntSet: a set of integers that can be iterated in ascending order in O(N) time

I needed a set with the following properties: can be iterated in ascending or descending order in \$O(N)\$ time the usual set operations (contains, add, remove, discard) can still be done in \$O(1)\$ ...
joseville's user avatar
  • 446
5 votes
2 answers
1k views

Sum numbers digit by digit

The problem goes like this: Given two one-dimensional arrays, for example a = (3, 4, 5) and b = (5, 6, 9), write a function ...
Eugene Yarmash's user avatar
1 vote
4 answers
183 views

Spelling Java long values to human readable text in English

Now, I have extended my program taking some answers in the previous iteration into account: ...
coderodde's user avatar
  • 27k
5 votes
4 answers
2k views

Converting int values in Java to human readable strings in English

(See also the next iteration.) I have rolled this short program that converts int values to the human readable strings: ...
coderodde's user avatar
  • 27k
0 votes
1 answer
1k views

Integer array to string

This function creates takes an int * buffer and creates a neatly formatted string (useful for printing the contents of an array). Is the code easy to follow? Is it ...
cgoates's user avatar
  • 155
0 votes
1 answer
118 views

C++20 Wrapper class for an unsigned integer type and a templated derived class that codes modular arithmetic type

The context is a library to represent numbers (in this case integers) by wheighted position by radix powers for every radix R, passed as template argument. These derived types are digits of radix R, ...
Earendil's user avatar
3 votes
1 answer
60 views

Quickly read numeric input

Do you have any criticisms or corrections or improvements? I need to read only numbers(and this numbers could be from 0 to 1000000) ...
Оля Швенда's user avatar
2 votes
1 answer
198 views

Encode non-negative integer to a string of ascii letters and digits

I want to encode a non-negative integer to ASCII letters and digits. I wrote below code for that, that works, but which I assume too immature to use in production. ...
Richard Neumann's user avatar
1 vote
1 answer
58 views

C code for discovering numbers with interesting/beautiful/symmetric properties

Below is a C code that generates 1000 random positive integers, for each number we check primality, palindromic property, and the square root of the sum of the digits. The purpose is to find/discover ...
Redsbefall's user avatar
  • 1,132
3 votes
1 answer
2k views

Format an integer with space as thousand separator

I need to format a number with space as thousand separator in my Java code for Android. How can these functions be improved? What are the pros and cons to the first recursive method? What are the ...
Ola Ström's user avatar
19 votes
4 answers
4k views

Minimal `printf` for integer types in x86 assembly

I'm writing a minimal C runtime targeting an old 32-bit Windows XP machine as a personal project. The C runtime provided by compilers is quite bloated. I wouldn't mind some library bloats up to ...
xiver77's user avatar
  • 421
4 votes
3 answers
801 views

C++ money class for basic banking application

I wanted to get my code reviewed as I have tried to implement a basic Money class to represent money values in c++. Can anyone tell me if I am implementing my basic arithmetic operator overloads ...
paigelarry342's user avatar
6 votes
3 answers
1k views

Count the numbers in a range in which the sum of the odds digits have the same parity of the evens one

As already written in the title I want to count the numbers in the range (x+1, y) where x and y are integers, in which the sum of the digits in odd places have the same parity of the ones in even ...
DarkSkull's user avatar
  • 163
0 votes
3 answers
697 views

Converting whole numbers to binary in C/C++

...
nates7's user avatar
  • 1
2 votes
3 answers
742 views

Convert string to integer and catch errors

I have attempted [and made one] function to convert string to integer and catch errors in pure c Working method The whole working method is present in code itself The header file The str2num.h here:- <...
Pear's user avatar
  • 77
1 vote
0 answers
56 views

Displaying Submitted Links Securely On Your Pages

I wanted to allow my website visitors (any Tom, Dick & Harry) submit their links to my webpage for output on my page. I thought it's best to parse user submitted urls before echoing their ...
Nut Cracking Dude's user avatar
1 vote
1 answer
88 views

A simple Java integer integer hash set - follow-up 2

(See the previous version.) Now I have this: com.github.coderodde.util.IntHashSet: ...
coderodde's user avatar
  • 27k
1 vote
0 answers
78 views

A simple Java integer hash set - follow-up

(See the previous version.) (See the next version.) After incorporating changes in the previous post, I came up with this implementation. However, I left hashing as it is. ...
coderodde's user avatar
  • 27k
6 votes
4 answers
1k views

A simple Java integer hash set

(See the next version.) The following data structure implements a hash table based set for int values: ...
coderodde's user avatar
  • 27k
1 vote
3 answers
86 views

Validating an integer with affixes in compile-time or runtime

I'm implementing a function called is_proper_integer that will check the string whether it is a valid integer literal by borrowing the idea from C++ grammar. ...
Desmond Gold's user avatar
1 vote
2 answers
546 views

Unsigned 64bit subtract-with-borrow in Standard C++

I'm writing a (compile-time-)fixed-width unsigned big integer. To implement subtraction, a subtract-with-borrow primitive is required. When efficient platform-specific implementation isn't available (...
yuri kilochek's user avatar
0 votes
3 answers
222 views

Luhn algorithm using Stream API

Example ...
DozezQuest's user avatar
2 votes
1 answer
406 views

Safe runtime numeric casts

The rationale behind this code is to implement a runtime-safe number conversions in situations when precision loss is possible, but is not expected. Example: passing a ...
Dmitry's user avatar
  • 121
2 votes
1 answer
289 views

Write a Kotlin-function isPrimeNumber

Exercise: Write a prime number-test isPrime(num: Int), which for integer m >= 2 checks, if the integer is a prime number or not. My solution: ...
michael.zech's user avatar
  • 4,212
0 votes
1 answer
46 views

optimize integer validation

I have some classes with properties, wich could only have numeric values in a given range (e.g. 0 to 8). This should be validated and wrong values prevented with an exception. The given behavior may ...
finder2's user avatar
  • 103
5 votes
2 answers
3k views

Represent integer as binary in C

To practice my C, I've written a small function in C to display an integer as an 8-bit binary number. Please let me know how to improve this function, and other C-style coding conventions. Thanks! <...
Linny's user avatar
  • 9,977
6 votes
3 answers
384 views

Fastest (in clock cycles) 16-bit x 16-bit unsigned integer division algorithm for ATMEGA1284?

I am trying to create an optimized 16-bit division algorithm for the AVR ATMEGA1284. The goal is to reduce the number of clock cycles as much as possible. AVR INSTRUCTION SET MANUAL: https://ww1....
user4574's user avatar
  • 179
2 votes
1 answer
1k views

C++: swapping bytes of an uint64_t

This question is about swapping bytes of an uint64_t value, or in other words, about converting between endianness of an integer value: ...
coderodde's user avatar
  • 27k
3 votes
1 answer
287 views

Integer version of c++ standard library midpoint

I was extremely annoyed by the lengthy, edge-case-galore explanation of integer version of C++ standard library mindpoint implementation here, so I made my own simple 2's complement version. I present ...
namark's user avatar
  • 61
4 votes
4 answers
2k views

Convert Integer to Binary String

This code takes a user input number which is a positive integer, and outputs a string representation of the number in binary. For example, input 2 outputs ...
nexus_2006's user avatar
2 votes
2 answers
4k views

Football management game in C++

Recently, I have been working on a College Football Manager game, in which you coach a team from the ground up. I have successfully made it to where you can win games, lose games, and change your team ...
Troy Cox's user avatar

1
2 3 4 5
8