Questions tagged [unsigned-integer]

A specific data type that uses all its bits to represent an integer value, consequently only 0 or a positive number.

In typed languages, an unsigned integer is a specific data type that uses all its bits to represent an integer value, consequently only 0 or a positive number. It can also represent integers twice the magnitude of the signed integers for the same width since no bit is used up to indicate the sign.

591 questions
6
votes
1 answer

Unsigned int fields seem wrongly typed to System.Int32 by System.Data.SQLite

Using System.Data.SQLite, I am creating a table with unsigned integer columns: @"CREATE TABLE widgets (" + @"id unsigned integer(10) PRIMARY KEY, " + @"fkey unsigned integer(10), " + ... and then insert values like INSERT INTO widgets (id,…
user2770141
  • 121
  • 5
5
votes
3 answers

Weird result after assigning 2^31 to a signed and unsigned 32-bit integer variable

As the question title reads, assigning 2^31 to a signed and unsigned 32-bit integer variable gives an unexpected result. Here is the short program (in C++), which I made to see what's going on: #include using namespace std; int main() { …
Rushil Paul
  • 2,010
  • 4
  • 26
  • 39
5
votes
4 answers

C++: Signed/unsigned mismatch when only using unsigned types

When I try to compile the following C++ program using the Visual Studio 2010 C++ compiler (X86) with warning level /W4 enabled, I get a signed/unsigned mismatch warning at the marked line. #include #include #include int…
Nubok
  • 3,502
  • 7
  • 27
  • 47
5
votes
1 answer

Is this bitwise conversion safe?

I have a situation where I need to pack 16 bits into a 64-bit number and later read them back as a signed integer in the range [ -32768, 32768 ). The method I have chosen for this is to compute the number as a signed 16-bit int, immediately cast it…
dscerutti
  • 159
  • 6
5
votes
2 answers

Difference between uint can be negative

I have a question about unsigned integer in C/C++. They and the results of operations on them should always be positive or equal to zero but it does not look the case with uint16_t difference. uint are defined in the C++ header cstdint. The next…
5
votes
1 answer

Dealing with unsigned integers

I know that unsigned integers are infamous and generally avoided by C++ devs. I have a class with two int member variables that should not contain negative values: . . . private: int m_Y_AxisLen; int m_X_AxisLen; . . . I have designed the…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
5
votes
2 answers

How do I reinterpret an unsigned long (DWORD) as a signed long in C++?

I want to reinterpret an unsigned long (actually, a DWORD) as a signed long. I tried: DWORD x; long y = reinterpret_cast(x); However, VC++2010 intellisense tells me "Invalid type conversion". Why? How do I fix it?
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
5
votes
2 answers

Check if the sum of two unsigned ints is larger than uint_max

Suppose I have two integers x and y, and I want to check whether their sum is larger than UINT_MAX. #define UINT64T_MAX std::numeric_limits::max() uint64_t x = foo(); uint64_t y = foo(); bool carry = UINT64T_MAX - x < y; That code will…
jcarpenter2
  • 5,312
  • 4
  • 22
  • 49
5
votes
2 answers

Optimizing portable 128-bit integer shifts on a 32-bit architecture

In my spare time I've been working on a utility library that among other things, supports signed/unsigned 128-bit integers. This library uses cpu-dispatching to utilize simd instructions in some cases, but requires a portable fallback so it will run…
Koby Duck
  • 1,118
  • 7
  • 15
5
votes
1 answer

Unsigned integers in assembly

Brand new to assembly need some help with unsigned arithmetic. Converting from a C program is that means anything. Using: Linux NASM x86 (32 Bit) I want to read in a number from the user. I want this number to be unsigned. When I enter a number…
frillybob
  • 600
  • 2
  • 12
  • 26
5
votes
4 answers

Why Do We have unsigned and signed int type in C?

I am a beginner in C . I have recently learned about 2's Complement and other ways to represent negative number and why 2's complement was the most appropriate one. What i want to ask is for example, int a = -3; unsigned int b = -3; //This is the…
5
votes
2 answers

Detecting if an unsigned integer overflow has occurred when adding two numbers

This is my implementation to detect if an unsigned int overflow has occurred when trying to add two numbers. The max value of unsigned int (UINT_MAX) on my system is 4294967295. int check_addition_overflow(unsigned int a, unsigned int b) { if (a…
Kingamere
  • 9,496
  • 23
  • 71
  • 110
5
votes
3 answers

Compiler Error: Invalid Conversion from int* to unsigned int* [-fpermissive]

I'm having the strangest issue today. I was working with an example online, and to my lack of surprise, it didn't work (they pretty much never do). I went about fixing it myself, but I seem to be stuck on this error: Error: Invalid Conversion from…
Boom
  • 2,465
  • 4
  • 19
  • 21
5
votes
2 answers

Unsigned int to unsigned long long well defined?

I wanted to see what was happening behind the scenes when an unsigned long long was assigned the value of an unsigned int. I made a simple C++ program to try it out and moved all the io out of main(): #include #include void…
asimes
  • 5,749
  • 5
  • 39
  • 76
5
votes
1 answer

unsigned integer addition and undefined behavior in C90

Solved! The relevant passage can be found in C90 ISO 9899:1990 6.1.2.5 Types: "[..] A computation involving unsigned operands can never overflow, because [...]" Therefore 9899:1990 6.3 can not apply and can therefore not be undefined…
Mark A.
  • 579
  • 4
  • 13