Questions tagged [signed]

In computing, signedness is a property of data types representing numbers in computer programs.

A numeric variable is signed if it can represent both positive and negative numbers, and unsigned if it can only represent non-negative numbers (zero or positive numbers).

As signed numbers can represent negative numbers, they lose a range of positive numbers that can only be represented with unsigned numbers of the same size (in bits) because roughly half the possible values are non-positive values. Unsigned variables can dedicate all the possible values to the positive number range.

For example, a Two's complement signed 16-bit integer can hold the values −32768 to 32767 inclusively, while an unsigned 16 bit integer can hold the values 0 to 65535. For this sign representation method, the leftmost bit (most significant bit) denotes whether the value is positive or negative (0 for positive, 1 for negative).

Reference

980 questions
491
votes
18 answers

Iteration over std::vector: unsigned vs signed index variable

What is the correct way of iterating over a vector in C++? Consider these two code fragments, this one works fine: for (unsigned i=0; i < polygon.size(); i++) { sum += polygon[i]; } and this one: for (int i=0; i < polygon.size(); i++) { sum…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
445
votes
17 answers

Signed versus Unsigned Integers

Am I correct to say the difference between a signed and unsigned integer is: Unsigned can hold a larger positive value and no negative value. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to…
ASDFdotASPX
255
votes
6 answers

Why is 0 < -0x80000000?

I have below a simple program: #include #define INT32_MIN (-0x80000000) int main(void) { long long bal = 0; if(bal < INT32_MIN ) { printf("Failed!!!"); } else { printf("Success!!!"); …
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
203
votes
6 answers

Is char signed or unsigned by default?

In the book "Complete Reference of C" it is mentioned that char is by default unsigned. But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signed by default. Which one is correct?
C Learner
  • 2,287
  • 2
  • 14
  • 7
159
votes
10 answers

C++ convert hex string to signed integer

I want to convert a hex string to a 32 bit signed integer in C++. So, for example, I have the hex string "fffefffe". The binary representation of this is 11111111111111101111111111111110. The signed integer representation of this is: -65538. …
Clayton
  • 6,089
  • 10
  • 44
  • 47
153
votes
11 answers

~x + ~y == ~(x + y) is always false?

Does this code always evaluate to false? Both variables are two's complement signed ints. ~x + ~y == ~(x + y) I feel like there should be some number that satisfies the conditions. I tried testing the numbers between -5000 and 5000 but never…
Steve
  • 4,446
  • 7
  • 35
  • 50
151
votes
6 answers

What is the difference between “int” and “uint” / “long” and “ulong”?

I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong?
Difference Engine
  • 12,025
  • 5
  • 21
  • 11
120
votes
2 answers

Why is std::ssize() introduced in C++20?

C++20 introduced the std::ssize() free function as below: template constexpr auto ssize(const C& c) -> std::common_type_t>; A possible…
John Z. Li
  • 1,893
  • 2
  • 12
  • 19
116
votes
2 answers

Should I use size_t or ssize_t?

At my code, I do not use int or unsigned int. I only use size_t or ssize_t for portable. For example: typedef size_t intc; // (instead of unsigned int) typedef ssize_t uintc; // (instead of int) Because strlen, string, vector... all use…
hgyxbll
  • 1,317
  • 2
  • 9
  • 9
114
votes
6 answers

How to convert signed to unsigned integer in python

Let's say I have this number i = -6884376. How do I refer to it as to an unsigned variable? Something like (unsigned long)i in C.
Lior
  • 5,841
  • 9
  • 32
  • 46
103
votes
6 answers

Signed/unsigned comparisons

I'm trying to understand why the following code doesn't issue a warning at the indicated place. //from limits.h #define UINT_MAX 0xffffffff /* maximum unsigned int value */ #define INT_MAX 2147483647 /* maximum (signed) int value */ /*…
Peter
  • 1,031
  • 2
  • 8
  • 4
98
votes
13 answers

Detecting signed overflow in C/C++

At first glance, this question may seem like a duplicate of How to detect integer overflow?, however it is actually significantly different. I've found that while detecting an unsigned integer overflow is pretty trivial, detecting a signed overflow…
Channel72
  • 24,139
  • 32
  • 108
  • 180
93
votes
7 answers

What happens if I assign a negative value to an unsigned variable?

I was curious to know what would happen if I assign a negative value to an unsigned variable. The code will look somewhat like this. unsigned int nVal = 0; nVal = -5; It didn't give me any compiler error. When I ran the program the nVal was…
ckv
  • 10,539
  • 20
  • 100
  • 144
88
votes
9 answers

How do I deal with "signed/unsigned mismatch" warnings (C4018)?

I work with a lot of calculation code written in c++ with high-performance and low memory overhead in mind. It uses STL containers (mostly std::vector) a lot, and iterates over that containers almost in every single function. The iterating code…
Andrew T
  • 5,549
  • 7
  • 43
  • 55
63
votes
10 answers

Google map signed api key errors in Android

When I switched from my debug map key to my signed map key my maps stop working. I get the following errors in logcat: 09-03 18:18:04.112: WARN/System.err(4073): IOException processing: 26 09-03 18:18:04.112: WARN/System.err(4073):…
Jen
  • 653
  • 1
  • 5
  • 5
1
2 3
65 66