Questions tagged [unsigned]

An unsigned variable is a variable that can only represent non-negative numbers.

An unsigned variable is a variable that can only represent non-negative numbers, as opposed to a signed variable which can represent both positive and negative numbers.

1251 questions
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
8 answers

Casting an array of unsigned chars to an array of floats

What is the best way of converting a unsigned char array to a float array in c++? I presently have a for loop as follows for (i=0 ;i< len; i++) float_buff[i]= (float) char_buff[i]; I also need to reverse the procedure, i.e convert from unsigned…
miki
8
votes
2 answers

Unsigned long and bit shifting

I have a problem with bit shifting and unsigned longs. Here's my test code: char header[4]; header[0] = 0x80; header[1] = 0x00; header[2] = 0x00; header[3] = 0x00; unsigned long l1 = 0x80000000UL; unsigned long l2 = ((unsigned long) header[0] <<…
Marcello
  • 145
  • 1
  • 5
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
8
votes
3 answers

In C, How do I calculate the signed difference between two 48-bit unsigned integers?

I've got two values from an unsigned 48bit nanosecond counter, which may wrap. I need the difference, in nanoseconds, of the two times. I think I can assume that the readings were taken at roughly the same time, so of the two possible answers I…
John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
8
votes
6 answers

Proper way to count down with unsigned

I am reading Carnegie Mellon slides on computer systems for my quiz. In the slide page 49 : Counting Down with Unsigned Proper way to use unsigned as loop index unsigned i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1]; Even better size_t i;…
Kalia Dona
  • 449
  • 1
  • 5
  • 11
8
votes
4 answers

unsigned becomes signed in if-statement comparisons?

I have searched this site for an answer and found many responses to unsigned/signed comparison but this problem is that only unsigned parameters are compared but still it works funny. The problem with the following code is that the first if-statment…
Wilmer
  • 83
  • 1
  • 3
8
votes
5 answers

Unsigned and Signed Values in C (Output)

signed int x = -5; unsigned int y = x; What is the value of y? How is this so?
sambhav jain
  • 91
  • 1
  • 2
8
votes
3 answers

How do you store unsigned 64-bit integers in SQL Server?

I work with an application which uses rather big numbers and I need to store data as an unsigned 64-bit integer. I prefer to just store it without worrying about bit manipulation or anything like that so different programs can use the data in…
danmine
  • 11,325
  • 17
  • 55
  • 75
8
votes
6 answers

C initialize array in hexadecimal values

I would like to initialize a 16-byte array of hexadecimal values, particularly the 0x20 (space character) value. What is the correct way? unsigned char a[16] = {0x20}; or unsigned char a[16] = {"0x20"}; Thanks
Kingamere
  • 9,496
  • 23
  • 71
  • 110
8
votes
4 answers

Why unsigned int contained negative number

What I know about unsigned numerics (unsigned short, int and longs), that It contains positive numbers only, but the following simple program successfully assigned a negative number to an unsigned int: 1 /* 2 *…
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
8
votes
2 answers

Assembly x86 registers signed or unsigned

I know this is a really simple question, but I couldn't find an answer to this. Are the registers in x86 assembly (eax, ebx edx etc) signed or unsigned? If they're signed by default, how does the computer know to treat the registers as unsigned if…
user3604597
  • 251
  • 1
  • 5
  • 8
8
votes
8 answers

acceptable fix for majority of signed/unsigned warnings?

I myself am convinced that in a project I'm working on signed integers are the best choice in the majority of cases, even though the value contained within can never be negative. (Simpler reverse for loops, less chance for bugs, etc., in particular…
Tobi
  • 78,067
  • 5
  • 32
  • 37
8
votes
3 answers

Convert an unsigned 16 bit int to a signed 16 bit int in C#

I'm writing a datalog parser for a robot controller, and what's coming in from the data log is a number in the range of 0 - 65535 (which is a 16 bit unsigned integer if I'm not mistaken). I'm trying to convert that to a signed 16 bit integer to…
Dylan Vester
  • 2,686
  • 4
  • 29
  • 40