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
1 answer

Do compilers avoid intermediate integral promotion or conversions?

Consider a class like: struct mystruct { constexpr operator char() {return x;} signed char x; }; and an operation like: mystruct m; m.x = /* something at runtime */ int i = 3 * m + 45ULL * m; Are compilers able to skip the temporary…
Vincent
  • 57,703
  • 61
  • 205
  • 388
5
votes
2 answers

Is casting a signed integer to a binary floating point number cheaper than the inverse operation?

I know from articles like "Why you should never cast floats to ints" and many others like it that casting a float to a signed int is expensive. I'm also aware that certain conversion instructions or SIMD vector instructions on some architectures…
hatch22
  • 797
  • 6
  • 18
5
votes
3 answers

Why does Integer.parseInt() remove 0 from string values?

I have been trying to investigate why Integer.parseInt() removes 0 and returns the remaining numbers. For example: @Test public void testInteger() { System.out.println(Integer.parseInt("01234")); …
Name Name
  • 175
  • 1
  • 2
  • 11
5
votes
1 answer

Java Reflection .getMethod int vs Integer

I have a class MyClass in package mypackage which looks like this: package mypackage; public class MyClass{ public static myMethod (Integer i) { return i + " is an Integer"; } public static myMethod (int i){ return i +…
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
5
votes
6 answers

how do i create a Method that accepts both String[] and Integer[] in java without method overloading?

Let's say you have an integer array and a string array. How can you write a SINGLE method printArray that can print all the elements of both arrays. The method should be able to accept both integer arrays or string arrays. NOTE.. I cannot use method…
SSR
  • 151
  • 2
  • 7
5
votes
3 answers

Big O - is n always the size of the input?

I made up my own interview-style problem, and have a question on the big O of my solution. I will state the problem and my solution below, but first let me say that the obvious solution involves a nested loop and is O(n2). I believe I found a O(n)…
user2410449
  • 373
  • 1
  • 2
  • 11
5
votes
2 answers

Choosing an integer type in core data

When I create models in core data, I'm always a little perplexed by which integer type I should choose—16, 32, 64. I'm almost always needing something for a simple, basic number: a count of people in the household, for my present case. Probably…
Steven Hovater
  • 1,389
  • 2
  • 12
  • 25
5
votes
4 answers

Any reason to assign -0?

I am going through some old C code using Lint which stumbled upon this line: int16_t max = -0; The Lint message is that the "Constant expression evaluates to 0 in operation '-'". Is there any reason why someone would use -0?
qnyz
  • 437
  • 4
  • 15
5
votes
1 answer

Python Infinite Integers

Python 3 integers have unlimited precision. In practice, this is limited by a computer's memory. Consider the followng code: i = 12345 while True: i = i * 123 This will obviously fail. But what will be the result of this? The entire RAM (and…
mcu
  • 3,302
  • 8
  • 38
  • 64
5
votes
1 answer

Haskell function seems to be limiting integer length - i thought it used bignums?

i've got a short haskell function here that is supposed to convert "ABCDEF" into 0x41,0x42,0x43,0x44,0x45,0x46 (their ascii values), then multiply them so it becomes 0x4142,4344,4546 but it seems to be limiting integer length - i thought haskell…
Chris
  • 39,719
  • 45
  • 189
  • 235
5
votes
6 answers

Using one variable to input 5 integers C#

I am using C# and i am trying to find the the average of 5 values but i can only use 2 variables. How can you input 5 integers into one variable and display the average of said integers
Callum97
  • 53
  • 7
5
votes
1 answer

Check if python int is too large to convert to float

Is there any way to check if a long integer is too large to convert to a float in python?
ad126
  • 51
  • 1
  • 2
5
votes
4 answers

Is this modern programming interview challenge's solution unreliable?

So I failed the programming interview question that is like "Given an array of ints 1, 2, ..., n with one of them missing, find the missing one." The interviewer said the correct answer is to sum the numbers and subtract the sum from n(n+1)/2,…
user5124106
  • 393
  • 2
  • 3
  • 11
5
votes
7 answers

Are there any solid large integer implementations in C?

I am working on a project where I need to crunch large integers (like 3^361) with absolute precision and as much speed as possible. C is the fastest language I am familiar with, so I am trying to code my solution in that language. The problem is…
sadakatsu
  • 1,255
  • 18
  • 36
5
votes
8 answers

Integer.parseInt(String str) java.lang.NumberFormatException: Errors

I keep getting number format expectations, even though I'm trimming the strings and they don't contain non numerical characters bizarrely it works for some numbers and not others. Below is an example of a string I get number format exception for.…
manic bubble
  • 147
  • 1
  • 3
  • 13