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

Does the unsigned keyword affect the result of sizeof?

Do C and C++ guarantee that the unsigned equivalent of a type has the same size? Example: size_t size = sizeof(unsigned int); Is the unsigned completely moot here?
typ1232
  • 5,535
  • 6
  • 35
  • 51
8
votes
5 answers

difference between unsigned short int and unsigned short

Is there a difference between unsigned short int and a unsigned short decleration in c and if so, what is it please ? I tried looking online, but couldn't find anything worthwhile. unsigned short int x1; unsigned short x2; Lastly, if there is a…
Goaler444
  • 2,591
  • 6
  • 35
  • 53
8
votes
2 answers

Declare an "unsigned T"

I want to use unsigned T in a template: template void signed2unsigned(const T* source, unsigned T* dest, size_t n) { do { intmax_t s=*source; s+=min(*source); *dest=s; ++dest; …
user877329
  • 6,717
  • 8
  • 46
  • 88
8
votes
7 answers

How do you get an unsigned long out of a string?

What's the safest and best way to retrieve an unsigned long from a string in C++? I know of a number of possible methods. First, converting a signed long taken from atol. char *myStr; // Initalized to some value somehow. unsigned long n =…
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
8
votes
1 answer

Will 'comparison between signed and unsigned integer expressions' ever actually result in errors?

Often an object I use will have (signed) int parameters (e.g. int iSize) which eventually store how large something should be. At the same time, I will often initialize them to -1 to signify that the object (etc) hasn't been setup / hasn't been…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
7
votes
2 answers

Whats the working difference between a signed char pointer and an unsigned one?

I can understand the difference between a signed char and an unsigned one. But dont the pointers of the corresponding type equivalent in their operation? Cos sizeof(char) and sizeof(unsigned char) is always the same ( Or are there any counter…
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
7
votes
7 answers

Does unsigned math require more CPU instructions?

Take an C++ integral variable i, and suppose that you're multiplying its value by 2. If i has signedness, I believe that the operation is somewhat equivalent, at least mathematically, to: i = i << 1; But if i's type is unsigned, then since unsigned…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
7
votes
4 answers

Convert int to unsigned short java

I have written a .obj parser in java to modelize 3D objects on iPhone. I would like to export the data as a binary file, which must be as small as possible. I have plenty of indices that would fit a unsigned short, but they are represented as int in…
Friedrik
  • 389
  • 1
  • 2
  • 12
7
votes
1 answer

Signed variant of size_t in standard C++ library

Is there a signed variant of size_t in standard C++? Meaning exactly same bit size as size_t but signed. Of course I can do: #include using signed_size_t = std::make_signed_t; but maybe there is already similar definition…
Arty
  • 14,883
  • 6
  • 36
  • 69
7
votes
2 answers

If an integer is signed by default, why does the signed keyword exist?

I'm curious why the signed keyword exists because I believe most integers and other variables are signed by default. Putting the signed keyword before int virtually does nothing. What is it used for, and why does it exist?
Comyar D
  • 172
  • 2
  • 9
7
votes
3 answers

Should I use "mul" or "imul" when multiplying a signed number to an unsigned number?

I have found out that both mul and imul can be used to multiply a signed number to an unsigned number. For example: global _start section .data byteVariable DB -5 section .text _start: mov al, 2 imul BYTE [byteVariable] You can replace imul…
user8315006
7
votes
4 answers

Compiler warning for unary operation on unsigned int

I have this sample code which generates the following warning (VS2008 compiler with SP1): warning C4146: unary minus operator applied to unsigned type, result still unsigned Code: void f(int n) { } int main() { unsigned int n1 = 9; …
Naveen
  • 74,600
  • 47
  • 176
  • 233
7
votes
5 answers

Unsigned negative primitives?

In C++ we can make primitives unsigned. But they are always positive. Is there also a way to make unsigned negative variables? I know the word unsigned means "without sign", so also not a minus (-) sign. But I think C++ must provide it.
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
7
votes
1 answer

How to convert an NSString to an unsigned int in Cocoa?

My application gets handed an NSString containing an unsigned int. NSString doesn't have an [myString unsignedIntegerValue]; method. I'd like to be able to take the value out of the string without mangling it, and then place it inside an NSNumber.…
Dave
  • 12,408
  • 12
  • 64
  • 67
7
votes
4 answers

Array's index and argc signedness

The C standard (5.1.2.2.1 Program startup) says: The function called at program startup is named main. [...] It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ } or with two parameters [...]…
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94