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

Converting 32-bit unsigned integer (big endian) to long and back

I have a byte[4] which contains a 32-bit unsigned integer (in big endian order) and I need to convert it to long (as int can't hold an unsigned number). Also, how do I do it vice-versa (i.e. from long that contains a 32-bit unsigned integer to…
Aviram
  • 3,017
  • 4
  • 29
  • 43
8
votes
4 answers

What is the difference between literals and variables in C (signed vs unsigned short ints)?

I have seen the following code in the book Computer Systems: A Programmer's Perspective, 2/E. This works well and creates the desired output. The output can be explained by the difference of signed and unsigned representations. #include int…
7
votes
2 answers

Why is numeric_limits::max() not equal to -1?

#include #include using namespace std; static_assert(-1 == numeric_limits::max()); // ok static_assert(-1 == numeric_limits::max()); // ok static_assert(-1 == numeric_limits::max()); // error int…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
7
votes
1 answer

Floating Point Monotonic Property

I was reading a book (CSAPP) . In the book it is written that- floating point addition satisfies the following monotonicity property : if a>=b then (x + a) >= (x+b) for any value a,b and x other than NaN. This property of real (and integer)…
7
votes
3 answers

VHDL assigning literals

I'm trying to use unsigned integers in VHDL with well defined bit widths. It seems VHDL does not like me trying to assign literal values to these types defined as: variable LCD_DATA: unsigned(19 downto 0) := 0; But in my IDE (Quartus), I get a…
Christopher Brown
  • 177
  • 1
  • 3
  • 12
7
votes
2 answers

adding unsigned int to int

#include int main () { using namespace std; unsigned int i = 4; int a = -40; cout<
user2408639
7
votes
9 answers

unsigned integers in C

While i am running the program below it outputs like 109876543210-1-2-3-4-5-6-78-9-10-11-12-and s0 on. Why so? What is the concept of unsigned integer? main () { unsigned int i; for (i = 10; i >= 0; i--) printf…
amitshree
  • 2,048
  • 2
  • 23
  • 41
7
votes
1 answer

Why do C implicit conversions operate like they do?

When an integer number is out of the type's range, the max value + 1 is added / subtracted (depends on which part of the range the number was). For example, unsigned short num = 65537; num will have a value of 1 (65536 was subtracted). My question…
Zakum
  • 491
  • 1
  • 5
  • 6
6
votes
3 answers

C++ unsigned and signed conversion

I've seen this kind of question asked before, but supplied answers don't clear everything for me. When this question is posted it is usually accompanied by next example: #include int main() { unsigned int u = 10; int i =…
Aisec Nory
  • 385
  • 1
  • 8
6
votes
2 answers

How to calculate pow(2,n) when n exceeds 64 in c++?

So, I am new to programming in c++ and i came across this question where i need to calculate pow(2,n)/2 where n>64 ? i tried using unsigned long long int but as the limit of the c++ is only 2^64. So is there any method to calculate this. Edit: 1 <…
6
votes
1 answer

How does var work for numbers?

I experimented today with how the compiler determines the types for numbers declared as var. var a = 255; //Type = int. Value = byte.MaxValue. Why isn't this byte? var b = 32767; //Type = int. Value = short.MaxValue. Why isn't this short? var c =…
David Klempfner
  • 8,700
  • 20
  • 73
  • 153
6
votes
2 answers

C++ template trait to specify any unsigned integral type

I’m trying to implement a function which accepts only unsigned integral types. Below is what I’ve tried so far. It works for "unsigned int", but why doesn’t this compile for an "unsigned short?" #include #include…
JimEli
  • 113
  • 1
  • 8
6
votes
1 answer

Why should the output of this C code be "no"?

I came across this question. #include int main(void) { // your code goes here unsigned int i = 23; signed char c = -23; if (i > c) printf("yes"); else printf("no"); return 0; } I am unable to…
asad_hussain
  • 1,959
  • 1
  • 17
  • 27
6
votes
1 answer

Applying modulo operation on a value of type int and unsigned integer

For example the code below int a = -7777; int b = 10; cout<< a % b<
skydoor
  • 25,218
  • 52
  • 147
  • 201
6
votes
3 answers

Unsigned int (primitive) and Integer (Object) usage in Java

I'm following the Java tutorial on Primitive Data Types. Early on, it states that In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1. Use the…
Pasaribu
  • 95
  • 6