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

Smoothing algorithm using integer arithmetic

The following code was taken from an Arduino tutorial on smoothing: int smooth(int data, float filterVal, float smoothedVal) { if (filterVal > 1) { filterVal = .99; } else if (filterVal <= 0) { filterVal = 0; } smoothedVal =…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
5
votes
2 answers

C++ Programs return int type, so why does return -1 return 255?

Note that I am running a linux machine, although I don't think the result is different on a windows (or other) machine. This is a simple question - C++ programs usually return a 32 bit int. If I return -1, and then print the result from the…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
5
votes
1 answer

Are there standard integer types with sizes being template parameters?

Suppose I need to make a template with a member of exactly N bits in length, where N is the template parameter. I could of course define something like this #include template struct sized_uint {}; template<> struct sized_uint<8> {…
Ruslan
  • 18,162
  • 8
  • 67
  • 136
5
votes
2 answers

Unusual Integer encoding to bytes - what scheme is this?

What Integer encoding scheme is used to achieve the following, and how can we do this in .Net: 127 = 7F 128 = 8001 255 = FF01 256 = 8002 500 = F403
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
5
votes
1 answer

Bypassing an unsigned addition overflow detected by CBMC

CBMC detects a possible unsigned addition overflow in the following lines: l = (t + *b)&(0xffffffffL); c += (l < t); I agree that there is a possibility of an overflow in the first line, but I am taking care of the carry in the next line which CBMC…
AcidBurn
  • 199
  • 1
  • 11
5
votes
4 answers

How do I convert a UITextField to an integer in Swift 2.0?

I'm a beginner at swift, and was doing some testing. To tell you the truth, I barely know what I'm doing yet, so please try to explain clearly. I was making a random number generator app & wanted to add in a uitextfield so that the user can type in…
Joe Maglione
  • 53
  • 1
  • 1
  • 3
5
votes
2 answers

Efficient comparison of small integer vectors

I have small vectors. Each of them is made of 10 integers which are between 0 and 15. This means that every element in a vector can be written using 4 bits. Hence I can concatenate my vector elements and store the whole vector in a single long type…
Issam T.
  • 1,677
  • 1
  • 14
  • 32
5
votes
2 answers

Java Integer out of range

I'm learning Java and I'm trying little programs. I have a problem with this one: /* Compute the number of cubic inches in 1 cubic mile. */ class Inches { public static void main(String args[]) { int ci; int im; im =…
Euriloco
  • 253
  • 1
  • 4
  • 9
5
votes
6 answers

C programming, why does this large array declaration produce a segmentation fault?

This code produces a segmentation fault during the array declaration. I'm confused as to why this happens. I intentionally selected 2000000000 as a value because it is below 2^31 and can fit into an integer variable. int main() { int …
aoeu
  • 1,128
  • 2
  • 13
  • 22
5
votes
1 answer

replacing numbers in a string with regex in javascript

I have string that contains numbers and characters. I want to replace the numbers with another value that will give it a css class of someClass. Now I got the code to detect all the numbers in the string and replace it with something else. My…
Burning Crystals
  • 1,157
  • 3
  • 19
  • 35
5
votes
6 answers

How do I get the absolute value of an integer without using Math.abs?

How do I get the absolute value of a number without using math.abs? This is what I have so far: function absVal(integer) { var abs = integer * integer; return abs^2; }
chaneyz
  • 95
  • 1
  • 1
  • 6
5
votes
2 answers

What is the regex for “Any positive integer, excluding 0”

There is already an other post for this but i can't comment on it. Sorry for that. I used var pattern = new RegExp('^[1-9]\d*$'); var result = fieldValue.search(pattern); but i get a "-1" if I put 12 It allows me just number from 1 to 9 and no…
Pierre
  • 675
  • 1
  • 8
  • 18
5
votes
5 answers

Find a pair of array elements with a given sum and product in O(n*log(n))

Let A be an array of n positive integers, and k a given integer. I'm looking for algorithm to find if there is a pair of elements in the array such that A[i] * A[j] == k and A[i] == A[j] + k. If there is such a pair, the algorithm should return…
Hades200621
  • 175
  • 1
  • 10
5
votes
1 answer

Appropriate data field type for true/false value?

What's the most appropriate (read least data consuming) data field to store a true/false/1/0 value in a mySQL database? I have previously used a one character long tinyint, but I am not sure if it's the best solution? Thanks!
Industrial
  • 41,400
  • 69
  • 194
  • 289
5
votes
2 answers

Bash if variable is an integer

I'm trying to write an if statement in bash that will exit if the variable supplied is not an integer. I will eventually be nesting this if statement within a while loop. When I do run this I am getting an syntax error. #!/bin/bash if [ $f1 !=…
thisguy
  • 51
  • 1
  • 2