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
29
votes
4 answers

Signed vs. unsigned integers for lengths/counts

For representing a length or count variable, is it better to use signed or unsigned integers? It seems to me that C++ STL tends to prefer unsigned (std::size_t, like in std::vector::size(), instead C# BCL tends to prefer signed integers (like in…
user1149224
28
votes
13 answers

Calculating bits required to store decimal number

Consider unsigned integer representation. How many bits will be required to store a decimal number containing: i) 3 digits ii) 4 digits iii) 6 digits iv) n digits I know that the range of the unsigned integer will be 0 to 2^n but I don't get how…
user379888
28
votes
2 answers

How do I declare 64bit unsigned int in dart/flutter?

For an app, I need a 64bit unsigned int. Looking at dart documentation I did not see how to exactly go about declaring one. Can anyone tell me how this is done? I will use this "64bit unsigned int" in bitwise operation.
Ragas
  • 3,005
  • 6
  • 25
  • 42
27
votes
2 answers

Is there a practical way of using natural numbers in Haskell?

I'm learning Haskell and would like to impose the use of positive integers (1,2,3, ...) in some constructors, but I only seem to find the 'Int' and 'Integer' datatypes. I could use the canonical data Nat = Zero | Succ Nat but then I couldn't use…
Seymour Kooze
  • 415
  • 5
  • 7
25
votes
4 answers

Expression 'i < 0' is always false

For the following snippet: size_t i = 0; std::wstring s; s = (i < 0) ? L"ABC" : L"DEF"; s = (i != -1) ? L"ABC" : L"DEF"; PVS-Studio analysis logs warning for the first condition i < 0, as expected: V547 Expression 'i < 0' is always false.…
mloskot
  • 37,086
  • 11
  • 109
  • 136
25
votes
3 answers

Subtraction between signed and unsigned followed by division

The following results make me really confused: int i1 = 20-80u; // -60 int i2 = 20-80; // -60 int i3 =(20-80u)/2; // 2147483618 int i4 =(20-80)/2; // -30 int i5 =i1/2; // -30 i3 seems to be computed as (20u-80u)/2, instead of…
Leo Lai
  • 863
  • 8
  • 17
24
votes
2 answers

Why is imul used for multiplying unsigned numbers?

I compiled the following program: #include uint64_t usquare(uint32_t x) { return (uint64_t)x * (uint64_t)x; } This disassembles to: 0: 89 f8 mov eax,edi 2: 48 0f af c0 imul rax,rax 6: c3 …
marmistrz
  • 5,974
  • 10
  • 42
  • 94
24
votes
4 answers

How to enable compiler warning when comparing char and unsigned char?

take for example the following C code : int main(int argc, char *argv[]) { signed char i; unsigned char count = 0xFF; for (i=0; i
sagi
  • 523
  • 3
  • 15
23
votes
10 answers

Why Is Comparing if an Unsigned Int >= 0 a "Pointless Comparison"?

I got warning: Pe186 "Pointless comparison of unsigned int with zero" when I tried to compile the following code: for(clLoop = cpLoopStart; clLoop >= 0; clLoop--) { //Do something } I don't understand why. I…
Josh
  • 255
  • 1
  • 2
  • 3
23
votes
5 answers

Advice on unsigned int (Gangnam Style edition)

The video "Gangnam Style" (I'm sure you've heard it) just exceeded 2 billion views on youtube. In fact, Google says that they never expected a video to be greater than a 32-bit integer... which alludes to the fact that Google used int instead of…
Chewco
  • 249
  • 2
  • 10
22
votes
7 answers

Converting char* to unsigned char*

How do I copy a char* to a unsigned char* correctly in C. Following is my code int main(int argc, char **argv) { unsigned char *digest; digest = malloc(20 * sizeof(unsigned char)); strncpy(digest, argv[2], 20); return 0; } I would…
Rajiv
  • 545
  • 1
  • 6
  • 12
21
votes
5 answers

why is char's sign-ness not defined in C?

The C standard states: ISO/IEC 9899:1999, 6.2.5.15 (p. 49) The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and…
Elazar Leibovich
  • 32,750
  • 33
  • 122
  • 169
21
votes
6 answers

Would it break the language or existing code if we'd add safe signed/unsigned compares to C/C++?

After reading this question on signed/unsigned compares (they come up every couple of days I'd say): Signed / unsigned comparison and -Wall I wondered why we don't have proper signed unsigned compares and instead this horrible mess? Take the…
Nordic Mainframe
  • 28,058
  • 10
  • 66
  • 83
20
votes
6 answers

C# multi-threaded unsigned increment

I want to increment an unsigned integer from multiple threads. I know about Interlocked.Increment, but it does not handle unsigned integers. I could use lock(), but I would rather not if possible for performance reasons. Is it thread safe just to…
FlappySocks
  • 3,772
  • 3
  • 32
  • 33
20
votes
5 answers

Why compiler is not giving error when signed value is assigned to unsigned integer? - C++

I know unsigned int can't hold negative values. But the following code compiles without any errors/warnings. unsigned int a = -10; When I print the variable a, I get a wrong value printed. If unsigned variables can't hold signed values, why do…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184