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
12
votes
5 answers

Unsigned and Signed Extension

Can someone explain the following code output to me: void myprint(unsigned long a) { printf("Input is %lx\n", a); } int main() { myprint(1 << 31); myprint(0x80000000); } output with gcc main.c : Input is ffffffff80000000 Input is…
Saksham Jain
  • 537
  • 3
  • 14
12
votes
4 answers

Is it safe to use negative integers with size_t?

I just saw some C++ code like this. It was using a condition to decide whether to walk forward or backward through a std::vector. The compiler doesn't complain, but I thought size_t was unsigned. Is this dangerous? vector v { 1,2,3,4,5 }; …
Rob N
  • 15,024
  • 17
  • 92
  • 165
12
votes
6 answers

What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

I'm dealing with some code at work that includes an expression of the form -(sizeof(struct foo)) i.e. the negation of a size_t, and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking…
Phil Miller
  • 36,389
  • 13
  • 67
  • 90
12
votes
1 answer

Unsigned minus unsigned difference between 32 and 64-bit

The situation: I have a piece of code that works when compiled for 32-bits but fails when compiled for 64-bits with gcc 4.6. After identifying the problem and reading up on the standards, I cannot really understand why it is working for 32-bits. I…
Leo
  • 2,328
  • 2
  • 21
  • 41
11
votes
14 answers

C reverse bits in unsigned integer

I'm converting an unsigned integer to binary using bitwise operators, and currently do integer & 1 to check if bit is 1 or 0 and output, then right shift by 1 to divide by 2. However the bits are returned in the wrong order (reverse), so I thought…
user1139103
11
votes
6 answers

Is unsigned char i equivalent to unsigned j?

In following program I used unsigned keyword. #include int main() { unsigned char i = 'A'; unsigned j = 'B'; printf(" i = %c j = %c", i, j); } Output: i = A j = B Is unsigned char i equivalent to unsigned j?
msc
  • 33,420
  • 29
  • 119
  • 214
11
votes
2 answers

Data type promotions during arithmetic operations: -1 < (unsinged int) 1 == false

main() { if ( -1 < (unsigned char) 1 ) printf("less than"); else printf("NOT less than"); } Prints less than. Because, (unsigned char) 1 is converted to (signed char) 1 and then: (signed) -1 < (signed) 1, thus output is…
bakra
  • 387
  • 4
  • 15
11
votes
14 answers

The importance of declaring a variable as unsigned

Is it important to declare a variable as unsigned if you know it should never be negative? Does it help prevent anything other than negative numbers being fed into a function that shouldn't have them?
Rarge
  • 155
  • 2
  • 5
11
votes
1 answer

Why would we use addiu instead of addi?

In MIPS assembly, what is the benefit of using addiu over addi? Isn't addiu unsigned (and will ruin our calculations?)
chickenman
  • 728
  • 2
  • 9
  • 29
11
votes
3 answers

How to convert signed 32-bit int to unsigned 32-bit int?

This is what I have, currently. Is there any nicer way to do this? import struct def int32_to_uint32(i): return struct.unpack_from("I", struct.pack("i", i))[0]
Claudiu
  • 224,032
  • 165
  • 485
  • 680
10
votes
5 answers

what's the point using unsigned int in C?

I thought that unsigned int could store only integers >= 0. But I tried assigning a negative to an unsigned int, nothing special happened. It seems like it stored the value with no problem. So what is the difference between signed and unsigned int,…
Danny
  • 125
  • 1
  • 7
10
votes
3 answers

Xcode 10: Where is the don't code sign option in Code Signing Identity

I'm building a Unity iOS app for a client in the form of an unsigned XCArchive as the client signs the XCArchive manually for security reasons. I was following this guide beforehand: How to build and sign an iOS app on separate machines? But I…
quaternion
  • 77
  • 2
  • 16
10
votes
2 answers

scanf %u negative number?

I have tried scanf("%u",&number) and I have entered negative number the problem is when I printf("%d",number) I get the negative number. I thought this will prevent me from reading negative number. Are scanf("%d",&number) and scanf("%u",&number) the…
rondino
  • 395
  • 2
  • 3
  • 10
10
votes
4 answers

*Might* an unsigned char be equal to EOF?

When using fgetc to read the next character of a stream, you usually check that the end-of-file was not attained by if ((c = fgetc (stream)) != EOF) where c is of int type. Then, either the end-of-file has been attained and the condition will fail,…
Rémi Peyre
  • 410
  • 3
  • 12
10
votes
2 answers

Something wrong with javascript unsigned shift operator on iPad?

I accidentally ran into what appears to be a very strange bug in Safari's javascript engine on iPad. The unsigned shift operator >>> is supposed to bitwise right-shift a number. I experienced some errors in a script that worked fine on other…
Rogier
  • 153
  • 5