Questions tagged [integer]

Common datatype in many programming languages for representing both negative and non-negative whole numbers. Use this tag for questions about using, storing, or manipulating integers. For 64-bit integers, use the [long-integer] tag instead.

An integer is a whole number that can be negative, positive, or zero. For example, -2, -1, 0, 1, 2. In many programming languages, the integer data type is commonly represented as 32-bits, but may also be 64-bits (Usually referred to as the Long data type).

For 32-bit integers:

  • The signed range is : -2147483648 to 2147483647.
  • The unsigned range is : 0 to 4294967295.

For 64-bit integers:

  • The signed range is : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • The unsigned range is : 0 to 18,446,744,073,709,551,615.

For further reading, see the Wikipedia article on integers.

13448 questions
5
votes
2 answers

How to convert negative integers to binary in Ruby

Question 1: I cannot find a way to convert negative integers to binary in the following way. I am supposed to convert it like this. -3 => "11111111111111111111111111111101" I tried below: sprintf('%b', -3) => "..101" # .. appears and does not show…
Noby Fujioka
  • 1,744
  • 1
  • 12
  • 15
5
votes
2 answers

Integer time complexity in Haskell

I had an assignment in school last week to implement a function for calculating the n:th number in the fibonacci sequence. A 'sub-assignment' was to implement it using accumulation(Might not be a correct translation) in order to give the function…
vichle
  • 2,499
  • 1
  • 15
  • 17
5
votes
2 answers

Int with no value (set int to no value)

I want to create an Int that has no value (an already existent variable). If I do this; number = Int() then it still has the value 0. I want nothing, a nonexistent value.
Rappe Stegarn
  • 347
  • 1
  • 4
  • 10
5
votes
7 answers

Java integer division doesn't give floor for negative numbers

I was trying to use java's integer division, and it supposedly takes the floor. However, it rounds towards zero instead of the floor. public class Main { public static void main(String[] args) { System.out.println(-1 / 100); // should be…
AMACB
  • 1,290
  • 2
  • 18
  • 26
5
votes
1 answer

Why are decimal and hexadecimal integer literals treated differently?

Reading Stanley Lippman's "C++ Primer", I learned that by default decimal integer literals are signed (smallest type of int, long or long long in which the literal's value fits) whereas octal and hexadecimal literals can be either signed or unsigned…
5
votes
4 answers

Get part of an integer in Python

Is there an elegant way (maybe in numpy) to get a given part of a Python integer, eg say I want to get 90 from 1990. I can do: my_integer = 1990 int(str(my_integer)[2:4]) # 90 But it is quite ugly. Any other option?
tagoma
  • 3,896
  • 4
  • 38
  • 57
5
votes
4 answers

Rounding integers routine

There is something that baffles me with integer arithmetic in tutorials. To be precise, integer division. The seemingly preferred method is by casting the divisor into a float, then rounding the float to the nearest whole number, then cast that back…
Adl A
  • 183
  • 1
  • 8
5
votes
2 answers

Most efficient way to convert values of column in Pandas DataFrame

I have a a pd.DataFrame that looks like: I want to create a cutoff on the values to push them into binary digits, my cutoff in this case is 0.85. I want the resulting dataframe to look like: The script I wrote to do this is easy to understand but…
O.rka
  • 29,847
  • 68
  • 194
  • 309
5
votes
4 answers

How to convert an integer to variable length byte string?

I want to convert an integer (int or long) a big-endian byte string. The byte string has to be of variable length, so that only the minimum number of bytes are used (the total length length of the preceding data is known, so the variable length can…
user141335
5
votes
2 answers

Efficient algorithm for conversion between numeral system

Is there any efficient algorithm for conversion between numeral system when the size of source integer is arbitrary? For example, assume that there is an integer array {1, 4, 8} which is 148 in decimal format as an input. It might be converted to…
summerlight
  • 882
  • 7
  • 16
5
votes
3 answers

Increment integer in NSUserDefaults

Is there a more elegant way to increment a counter stored in user defaults? let defaults = NSUserDefaults.standardUserDefaults() defaults.setInteger(defaults.integerForKey("counter")+1, forKey: "counter")
Henrik
  • 3,908
  • 27
  • 48
5
votes
2 answers

Using enum With Integers in Swift

I'd like to know how can I limit the set of values that I can pass to function as an argument (or to class as a property). Or, in other words, the logic that I want to implement, actually, is to make function or a class accept only particular…
phbelov
  • 2,329
  • 3
  • 18
  • 15
5
votes
2 answers

Storing variables as single bits

for the past few weeks i have been working on making a program that can make prime spirals as efficient as possible. I looked into multithreading to increase the speed of the program, and now i've run into a new problem. My list of primes has a…
user5887651
5
votes
1 answer

Exactness of integer square root in Python

I would like to understand why something is happening. I needed to implement the integer square root in Python (isqrt(64) = 8 = isqrt(80)). I was convinced that the naive approach: def isqrt(n): return int(math.sqrt(n)) was bound to fail…
zeycus
  • 860
  • 1
  • 8
  • 20
5
votes
8 answers

Sum Integers written into a String

I used int num = Integer.parseInt(str) to written integer values into a string.I need a function that reads these values from given string and calculates their sum. Example: input - "43 68 9 23 318" output - 461
slaviyy
  • 59
  • 1
  • 1
  • 2