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

Prevent user passing negative numbers to a function accepting unsigned int

So here's the code: int create_mask(unsigned b, unsigned e) { unsigned int mask=1; if(b= 0.\n"); …
zubergu
  • 3,646
  • 3
  • 25
  • 38
5
votes
2 answers

C# int32 literal can only be stored in long data type

Im preparing for a very tricky c# exam and this question popped up while doing so. I have the following code: uint zzz = -12u; -12u is recognized as System.Uint32 literal but it can only be stored in variable of type long. Why is that ?
Chris Nickson
  • 105
  • 2
  • 3
  • 8
4
votes
4 answers

C++: Difference of two unsigned 64-bit integer in a signed 64-bit integer

I am trying to write a function in C++ which takes two 64 bit unsigned integers and returns their difference in a signed 64 bit integer. It seems to be a bit complicated because of the overflow situation - Since the inputs are two unsigned positive…
Abhi
  • 1,167
  • 2
  • 14
  • 21
4
votes
3 answers

Why do `(char)~0` and `(unsigned char)~0` return values of different widths?

I bumped into this while writing a program trying to print the constituent byte values of UTF-8 characters. This is the program that I wrote to test the various ~0 operations: #include int main() { printf("%x\n", (char)~0); //…
4
votes
1 answer

Why is this LCG significantly faster in Python 2.7 than in Python 3.x?

This is a simple linear congruential generator in Python: def prng(n): # https://en.wikipedia.org/wiki/Lehmer_random_number_generator while True: n = n * 48271 % 0x7fffffff yield n g = prng(123) for i in range(10**8): …
wim
  • 338,267
  • 99
  • 616
  • 750
4
votes
3 answers

Is there a non-looping unsigned 32-bit integer square root function C

I have seen floating point bit hacks to produce the square root as seen here fast floating point square root, but this method works for floats. Is there a similar method for finding the integer square root without loops of a 32-bit unsigned integer?…
yosmo78
  • 489
  • 4
  • 13
4
votes
1 answer

why does linux kernel (often) hold pointer in unsigned long object

I often see (in Linux kernel for example) that unsigned long is used to hold the pointers. I wonder what is the reason for this given that size of a pointer may be larger than integer type (including long). Is it portable to keep a pointer in…
Mark
  • 6,052
  • 8
  • 61
  • 129
4
votes
4 answers

Why does C not run a comparison of unsigned int with negative value?

Consider this C code: #include "stdio.h" int main(void) { int count = 5; unsigned int i; for (i = count; i > -1; i--) { printf("%d\n", i); } return 0; } My observation/question: the loop never gets executed. But if I…
Jaanus
  • 17,688
  • 15
  • 65
  • 110
4
votes
2 answers

Does the position of this bit-wise operator change the behavior?

Are these two lines of code equivalent? P1->OUT &= ~(uint8_t)(1<<1); P1->OUT &= (uint8_t)(~(1<<1));
4
votes
2 answers

C compiler not recognizing unsigned long

On my computer, long takes a maximum value of 9223372036854775807. However, when I tired unsigned long with a larger value the compiler throws a warning saying it needs to be interpreted as an unsigned long when I already had it defined as so. Why…
4
votes
1 answer

Method that returns uint16_t array properly in C++

I have my C# code that returns uint array but I want to do it in C++. I looked other posts; they use uint pointer array where my array is not. Does anyone know how to return uint16_t array properly? This is C# code works fine public static…
Selen
  • 210
  • 2
  • 16
4
votes
1 answer

Converting 32-bit integer value to unsigned 16-bit integer

What does the C standard say about: uint32_t a = 0x11223344; uint16_t b = a; When printed, I am getting 0x3344, which makes sense; so this must be legal and correct behaviour?
Mark
  • 6,052
  • 8
  • 61
  • 129
4
votes
1 answer

Signed or unsigned loop counter

I was very surprised by the difference between using signed and unsigned loop counter in this simple example: double const* a; __assume_aligned(a, 64); double s = 0.0; //for ( unsigned int i = 0; i < 1024*1024; i++ ) for ( int i = 0; i < 1024*1024;…
4
votes
2 answers

If I decrement `std::size_t(0)` is that guaranteed to be equal to `std::size_t(-1)`?

Here's evidence that it is: inline constexpr std::size_t prev(std::size_t i) { --i; return i; } int main() { static const std::size_t i = 0; static_assert(prev(i) == std::size_t(-1), "Decrementing should give std::size_t(-1)"); …
Ben
  • 9,184
  • 1
  • 43
  • 56
4
votes
4 answers

unsigned int and signed char comparison

I am trying to compare an unsigned int with a signed char like this: int main(){ unsigned int x = 9; signed char y = -1; x < y ? printf("s") : printf("g"); return 0; } I was expecting the o/p to be "g". Instead, its "s". What kind of…
badmaash
  • 4,775
  • 7
  • 46
  • 61