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
16
votes
3 answers

Why does std::abs return signed types

I'm getting warning for signed vs. unsigned comparison when I'm comparing a std::abs(int) against an unsigned. And indeed, std::abs returns signed values. Why was that choice made? It would have solved the problem of negative values whose…
akim
  • 8,255
  • 3
  • 44
  • 60
15
votes
3 answers

When should I use std_logic_vector and when should I use other data types?

I'm new to VHDL and am having trouble figuring out which data types are appropriate to use where. If I understand correctly, for synthesis, all top level entity ports should be declared either std_logic_vector or std_logic and never any other…
Emil Eriksson
  • 2,110
  • 1
  • 21
  • 31
15
votes
5 answers

How to detect encodings on signed integers in C?

The ISO C standard allows three encoding methods for signed integers: two's complement, one's complement and sign/magnitude. What's an efficient or good way to detect the encoding at runtime (or some other time if there's a better solution)? I want…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
14
votes
1 answer

Does the aliasing loophole apply to signed characters?

In C++ there is an aliasing loophole which allows the object representation of any object to be read or written through some pointers of character type. Does this apply only to char and unsigned char or also to signed char?
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
14
votes
3 answers

Why is there one more negative int than positive int?

The upper limit for any int data type (excluding tinyint), is always one less than the absolute value of the lower limit. For example, the upper limit for an int is 2,147,483,647 and ABS(lower limit) = 2,147,483,648. Is there a reason why there is…
Mark He
  • 735
  • 7
  • 14
14
votes
1 answer

Coldfusion CFHTTP with SHA512-hmac signed REST request body

I am trying to make a signed request to the trading API at bitfloor.com (it's a REST API) Bitfloor gives me: 1) API Key (i.e. 6bd2b780-00be-11e2-bde3-2837371c3c3a) 2) Secret Key (i.e.…
Jay
  • 173
  • 6
14
votes
4 answers

In C Left shift (char) 0xFF by 8 and cast it to int

On left shift of (char) 0xff by 8 and casting it to int we get -256 or 0xffffff00. Can somebody explain why this should happen? #include int main (void) { char c = 0xff; printf("%d %x\n", (int)(c<<8),(int)(c<<8)); return…
Geos
  • 1,471
  • 4
  • 14
  • 16
13
votes
2 answers

Signedness aliasing using reinterpret_cast

Take the following code #include void func() { int i = 2147483640; while (i < i + 1) { std::cerr << i << '\n'; ++i; } return; } int main() { func(); } This code is clearly wrong, as the while…
Jonas Müller
  • 317
  • 1
  • 8
13
votes
4 answers

What is the difference between signed and unsigned binary

I've been reading a few sites, but none of them make sense to me. Is signed and unsigned binary them same as signed and unsigned variables. I'd be glad if you could help :)
R.M.R
  • 193
  • 1
  • 2
  • 9
13
votes
2 answers

Little-Endian Signed Integer

I know the WAV file format uses signed integers for 16-bit samples. It also stores them in little-endian order, meaning the lowest 8 bits come first, then the next, etc. Is the special sign bit on the first byte, or is the special sign bit always on…
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
13
votes
4 answers

1's complement using ~ in C/C++

I am using Visual Studio 2013. Recently I tried the ~ operator for 1's complement: int a = 10; cout << ~a << endl; Output is -11 But for unsigned int a = 10; cout << ~a << endl; the output is 4294967296 I don't get why the output is -11 in the…
user2912611
  • 534
  • 2
  • 6
  • 16
13
votes
7 answers

how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

I am reading the book: CS-APPe2. C has unsigned and signed int type and in most architectures uses two's-complement arithmetic to implement signed value; but after learning some assembly code, I found that very few instructions distinguish between…
tomwang1013
  • 1,349
  • 2
  • 13
  • 27
13
votes
4 answers

Invalid conversion from unsigned char* to char*

Here is a code - 1 int main(int argc, char *argv[]) 2 { 3 signed char S, *psc; 4 unsigned char U, *pusc; 5 char C, *pc; 6 7 C = S; 8 C = U; 9 10 pc = psc; 11 pc = pusc; 12 13 return 0; 14…
nightlytrails
  • 2,448
  • 3
  • 22
  • 33
12
votes
7 answers

C++ How to combine two signed 8 Bit numbers to a 16 Bit short? Unexplainable results

I need to combine two signed 8 Bit _int8 values to a signed short (16 Bit) value. It is important that the sign is not lost. My code is: unsigned short lsb = -13; unsigned short msb = 1; short combined = (msb << 8 )| lsb; The result I get is…
Natalie
  • 445
  • 2
  • 5
  • 18
12
votes
2 answers

c printf signed float

What is the formatter to make sure that + or - signs are always shown in front of the float value in printf() in C? I haven't done C in a while, so where can I find a good reference on the web, any suggestions are appreciated
7oso
  • 283
  • 1
  • 3
  • 9