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

Signed Integer Network and Host Conversion

I would like to convert a int32_t from host byte order to network byte order and vice versa. I know about the htonl() function and its variants, but this takes unsigned integers. Is there a standard library function which can do the same with signed…
v1Axvw
  • 3,054
  • 3
  • 26
  • 40
9
votes
11 answers

Programmatically determining max value of a signed integer type

This related question is about determining the max value of a signed type at compile-time: C question: off_t (and other signed integer types) minimum and maximum values However, I've since realized that determining the max value of a signed type…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
9
votes
4 answers

Why must loop variables be signed in a parallel for?

I'm just learning OpenMP from online tutorials and resources. I want to square a matrix (multiply it with itself) using a parallel for loop. In IBM compiler documentation, I found the requirement that "the iteration variable must be a signed…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
9
votes
3 answers

How do I convert hex string into signed integer?

I'm getting a hex string that needs to be converted to a signed 8-bit integer. Currently I'm converting using Int16/Int32, which will obviously not give me a negative value for an 8-bit integer. If I get the value 255 in Hex, how do I convert that…
alexD
  • 2,368
  • 2
  • 32
  • 43
9
votes
2 answers

Consensus? MySQL, Signed VS Unsigned Primary/Foreign Keys

I have seen a couple threads about what everyone was doing now a days in regards to signed vs unsigned key values. It would seem that unsigned is optimal since it allows twice the number of rows for the same cost. Are there any benefits with signed…
Parris
  • 17,833
  • 17
  • 90
  • 133
9
votes
4 answers

Unsigned versus signed numbers as indexes

Whats the rationale for using signed numbers as indexes in .Net? In Python, you can index from the end of an array by sending negative numbers, but this is not the case in .Net. It's not easy for .Net to add such a feature later as it could break…
simendsjo
  • 4,739
  • 2
  • 25
  • 53
9
votes
1 answer

How does adding MIN_VALUE compare integers as unsigned?

In Java the int type is signed, but it has a method that compares two ints as if they were unsigned: public static int compareUnsigned(int x, int y) { return compare(x + MIN_VALUE, y + MIN_VALUE); } It adds Integer.MIN_VALUE to each argument,…
Boann
  • 48,794
  • 16
  • 117
  • 146
9
votes
1 answer

Why do we define INT_MIN as -INT_MAX - 1?

AFAIK this is a standard "idiom" # define INT_MIN (-INT_MAX - 1) # define INT_MAX 2147483647 Question: Why is the definition of INT_MIN not as -2147483648?
Cratylus
  • 52,998
  • 69
  • 209
  • 339
9
votes
1 answer

Properly handling the comparison of signed and unsigned values

I arrived at a point where I need to compare singed and unsigned values. Until now I always modified the code base to avoid this situation completely, but now I can't do that. So what is the really proper way to handle singed and unsigned…
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
9
votes
3 answers

How to cast a 32-bit integer from unsigned to signed in MySQL or PHP?

I have a string in PHP (came from some data source), which represents a formatted unsigned 32-bit integer. I need to store it into a MySQL database as a signed 32-bit integer, so that later I can retrieve it from PHP and use it as a (possibly…
Alex
  • 707
  • 1
  • 8
  • 14
8
votes
7 answers

Java negative int to hex and back fails

public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE; String minHex = Integer.toHexString(Integer.MIN_VALUE); System.out.println(min + " " + minHex); …
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
8
votes
1 answer

Packing two shorts into one int, dealing with negative and positive

I'm making a class PackedUnsigned1616 which stores two unsigned shorts in one int, and a class PackedSigned1616 which stores two signed shorts in one int. I've read up on bitwise operations, but I'm still confused on how to deal with signed and…
Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
8
votes
4 answers

Is it possible to test whether a type supports negative zero in C++ at compile time?

Is there a way to write a type trait to determine whether a type supports negative zero in C++ (including integer representations such as sign-and-magnitude)? I don't see anything that directly does that, and std::signbit doesn't appear to be…
user9723177
8
votes
2 answers

Magic number did not match while generating signed apk

I am trying to generate signed apk but I am getting the following error. I have searched about it but didn't find anything that can resolve this problem. Solution I've tried Invalidate caches/restart deleting .gradle folder uninstalling android…
Jay
  • 497
  • 6
  • 17
8
votes
1 answer

Why in C language for every signed int type must there be a corresponding unsigned int type?

I was reading C in a Nutshell and found this: "If an optional signed type (without the prefix u) is defined, then the corresponding unsigned type (with the initial u) is required, and vice versa." The paragraph is about The integer types with…
eddybudge
  • 165
  • 1
  • 8