# Questions tagged [unsigned-integer]

188 questions

1

votes

2

answer

6.4k

Views

### How to compare two unsigned integers (uint8_t) in c

I have two uint8_t pointers in my program and I want to compare the values of those. I don't have good idea to deal with unsigned ints. Here is my code
static void bacast_signed_message()
{
uint8_t *M = malloc(MAX_M_LEN*sizeof(uint8_t));//uint8_t M[MAX_M_LEN];//uint8_t *M;
int M_len = MAX_M_LEN;;
ui...

1

votes

1

answer

141

Views

### How to get an unsigned integer from a byte array in Kotlin JVM?

Kotlin 1.3 introduced unsigned integer types, but I can't seem to figure out how to get an unsigned integer from a ByteArray in Kotlin JVM.
Kotlin Native has a convenient ByteArray.getUIntAt() method, but this does not exist for Kotlin JVM.
val bytes: ByteArray = byteArrayOf(1, 1, 1, 1)
val uint: UI...

21

votes

5

answer

1.8k

Views

### Is a negative integer summed with a greater unsigned integer promoted to unsigned int?

After getting advised to read "C++ Primer 5 ed by Stanley B. Lipman" I don't understand this:
Page 66. "Expressions Involving Unsigned Types"
unsigned u = 10;
int i = -42;
std::cout

1

votes

1

answer

430

Views

### Understanding this 16-bit PRNG

I found this algorithm for a 16-bit PRNG. I don't understand what x, y and t are. What I want to do is use a 16-bit seed to generate multiple random 16-bit values.
If I'm correct, the function shown in that webpage (quoted below) is only pseudo code, since as it stands, it will always generate the s...

1

votes

1

answer

872

Views

### wcstombs_s “Cannot convert argument” DirectX-11

Fist things first - Full error:
Error 3 error C2664: 'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)' : cannot convert argument 1 from 'unsigned int *' to 'size_t *' C:\Users\Adam\Desktop\DirectxTEST\Win32Project4\D3dclass.cpp 88 1 Win32Project4
I know whats causing t...

1

votes

1

answer

156

Views

### Can we and how safe is to “signed” to “unsigned” trick to save one comparison in this case?

For example
bool CheckWithinBoundary(int x, int b) {
return (x>=0) && (x

1

votes

3

answer

718

Views

### Converting signed int to unsigned short with negative values being set to zero in C

I would like to know if there is an efficient way of converting a signed integer to an unsigned short where negative integer values are simply set to 0 in the unsigned short (in C ANSI). I know that this can be done with a simple if statement like the following:
int val1;
unsigned short val2;
val1=-...

1

votes

5

answer

2k

Views

### Converting unsigned char to signed int

Consider the following program
void main(){
char t = 179;
printf("%d ",t);
}
Output is -77.
But binary representation of 179 is
10110011
So, shouldn't output be -51, considering 1st bit is singed bit.
Binary representation of -77 is
11001101
It seems bit order is reversed. What's going on? Please so...

3

votes

4

answer

639

Views

### C/GL: Using -1 as sentinel on array of unsigned integers

I am passing an array of vertex indices in some GL code... each element is a GLushort
I want to terminate with a sentinel so as to avoid having to laboriously pass the array length each time alongside the array itself.
#define SENTINEL ( (GLushort) -1 ) // edit thanks to answers below
:
GLushort ve...

1

votes

1

answer

63

Views

### Error in storing variable values with size 8bits (uint8) in Matlab

I'm implementing a 128-bit AES encryption algorithm in Matlab, I'm implementing this code based on a functional code in C, the big problem is that in the C code all variables are defined as unsigned char making it easier to manipulate , In MatLab I declared all variables as uint8 (unsingned integer...

16

votes

6

answer

64.8k

Views

### C - unsigned int to unsigned char array conversion

I have an unsigned int number (2 byte) and I want to convert it to unsigned char type. From my search, I find that most people recommend to do the following:
unsigned int x;
...
unsigned char ch = (unsigned char)x;
Is the right approach? I ask because unsigned char is 1 byte and we casted from 2 byt...

17

votes

3

answer

28.6k

Views

### JNI: converting unsigned int to jint

How do I convert an unsigned int to jint? Do I have to convert it at all, or can I just return it without any special treatment? This is basically my code right now, but I can't test it, as I haven't setup JNI locally.
JNIEXPORT jint JNICALL
Java_test_test(JNIEnv* env, jobject obj, jlong ptr)
{
MyO...

2

votes

3

answer

942

Views

### Pointer to different data type in C

I have compiled and run the following program in C:
#include
#include
#include
int main(int argc, char* argv[]){
uint8_t data[8] = {0, 1, 2, 3, 4, 5, 6, 7};
uint32_t* pointer32 = &data[0];
uint64_t* pointer64 = &data[0];
printf("%" PRIu64 "\n", *pointer64);
printf("%" PRIu32 "\n", *(pointer32++))...

6

votes

4

answer

3.5k

Views

### QSpinBox with Unsigned Int for Hex Input

There are a variety of questions written here about QSpinBox's limitation of using an int as its datatype. Often people want to display larger numbers. In my case, I want to be able to show an unsigned 32bit integer in hexadecimal. This means I'd like my range to be [0x0, 0xFFFFFFFF]. The largest a...

1

votes

2

answer

8.2k

Views

### comparison of signed and unsigned numbers [duplicate]

This question already has an answer here:
Comparison operation on unsigned and signed integers
7 answers
Here is the question as shown below and the answers comes as True.I know there is some promotion which would happen when you compare an signed and unsigned.Can you please tell me how does the si...

1

votes

2

answer

8.1k

Views

### Unsigned int into a char array. Alternative to itoa?

I have a question about unsigned ints.
I would like to convert my unsigned int into a char array. For that I use itoa. The problem is that itoa works properly with ints, but not with unsigned int (the unsigned int is treaded as a normal int).
How should I convert unsigned int into a char array?
Than...

19

votes

1

answer

44.8k

Views

### How can I get the size of an std::vector as an int?

I tried:
#include
int main () {
std::vector v;
int size = v.size;
}
but got the error:
cannot convert 'std::vector::size' from type 'std::vector::size_type (std::vector::)() const noexcept' {aka 'long unsigned int (std::vector::)() const noexcept'} to type 'int'
Casting the expression to int like t...

1

votes

2

answer

5.2k

Views

### comparison between unsigned int and unsigned char

I have an unsigned char array, such as Data[2]. I needed it to compare with an output of a function returning unsigned int.
i tried to cast the Data[2] into unsigned int and vice versa. It didn't work.
What I am trying to do is:
if (Data[2] == ReadFlash2(40))
{
//Do Something.
}
ReadFlash2 is a func...

1

votes

6

answer

131

Views

### How to safely compare two unsigned integer counters?

We have two unsigned counters, and we need to compare them to check for some error conditions:
uint32_t a, b;
// a increased in some conditions
// b increased in some conditions
if (a/2 > b) {
perror("Error happened!");
return -1;
}
The problem is that a and b will overflow some day. If a overflowed...

6

votes

1

answer

126

Views

### Why is the output of this C code is “no”? [duplicate]

This question already has an answer here:
unsigned int and signed char comparison
5 answers
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 understand why...

2

votes

2

answer

4.1k

Views

### Representing n-bit unsigned integers in mysql

How do I represent this data in mysql?
16 bit unsigned integer -----Range: 0x0000 - 0xFFF7
64 bit unsigned int. Depicted as xx:xx:xx:xx:xx:xx:xx:xx -----Range: 0x0000 - 0xFFFFFFFFFFFFFFFF
2 bits ----- 00 - None, 01 - Residential Security, 10 - High Security
32 bit unsigned int
Should I convert e...

2

votes

2

answer

310

Views

### converting from size_t to unsigned int

Is it possible that converting from size_t to unsigned int result in overflow .
size_t x = foo ( ) ; // foo ( ) returns a value in type size_t
unsigned int ux = (unsigned int ) x ;
ux == x // Is result of that line always 1 ?
language : c++
platform : any

5

votes

4

answer

1.8k

Views

### 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 main(int argc, char **argv)
{
size_t idx = 42;
uint8_t bytesCount = 20;
// wa...

5

votes

3

answer

3.5k

Views

### 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()
{
unsigned long long n = 1

11

votes

3

answer

12.7k

Views

### C++ Implicit Conversion (Signed + Unsigned)

I understand that, regarding implicit conversions, if we have an unsigned type operand and a signed type operand, and the type of the unsigned operand is the same as (or larger) than the type of the signed operand, the signed operand will be converted to unsigned.
So:
unsigned int u = 10;
signed i...

3

votes

2

answer

62

Views

### Is there a way to make GCC alert me when I provide a signed integer to function that accepts only unsigned one?

This code gives me no warnings when compiled with g++:
unsigned int myFunc(unsigned int integer) {
return integer;
}
int main() {
int x = -1;
std::cout

2

votes

3

answer

103

Views

### why is a negative long value greater than a positive unsigned long value?

Example code:
long num1 = -1;
unsigned long num2 = 1;
if (num1 > num2)
printf("Num1 is greater\n");
else
printf("Num2 is greater\n");
Why does this evaluate num1 > num2 as true instead of it being false?

1

votes

1

answer

587

Views

### OpenCV Mat::zeros not filled with 0

I have this code:
Mat Marker = Mat::zeros(MarkerSize, MarkerSize, CV_8UC1);
cout

5

votes

1

answer

667

Views

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

For example the code below
int a = -7777;
int b = 10;
cout

1

votes

3

answer

1.5k

Views

### Convert Integer to Unsigned Long

Trying to use the function htonl() for a program that sends an init message to a server, along with an integer value, say 5. However, htonl() needs the following uint32_t integer hostlong
How can I convert 5 to an unsigned integer?

22

votes

2

answer

911

Views

### Is comparing an underflowed, unsigned integer to -1 well-defined?

Consider the following†:
size_t r = 0;
r--;
const bool result = (r == -1);
Does the comparison whose result initialises result have well-defined behaviour?
And is its result true, as I'd expect?
This Q&A was written because I was unsure of two factors in particular.
They may both be identified by...

1

votes

3

answer

765

Views

### Issue with uint64_t type

say I have the the following union :
typedef union
{
char array[8];
uint64_t u64;
} my_type ;
I want to shift one bit 1 through all the 64 bits the reserved, here is what I've tried :
...........................
my_type vector ={0};
short index =0;
for ( index=0 ; index

4

votes

1

answer

313

Views

### U suffix in the variable declaration

I know that if the number is followed with U suffix it is treated as unsigned. But why following program prints correct value of variable i even though it is initialized with negative value. (Compiled with gcc 4.9.2, 4.8.2, & 4.7.1)
Program1.cpp
#include
int main()
{
int i=-5U;
std::cout

2

votes

4

answer

6.1k

Views

### Function of type unsigned int returns negative number

Wow I thought I knew my C++ but this is weird
This function returns an unsigned int so I thought that means I will never get a negative number returned right?
The function determines how many hours ahead or behind of UTC you are. So for me I'm in Australia, Sydney so I am +10 GMT which means I am UT...

12

votes

7

answer

6.1k

Views

### How to generate random 64-bit unsigned integer in C

I need generate random 64-bit unsigned integers using C. I mean, the range should be 0 to 18446744073709551615. RAND_MAX is 1073741823.
I found some solutions in the links which might be possible duplicates but the answers mostly concatenates some rand() results or making some incremental arithmetic...

3

votes

3

answer

1.4k

Views

### unsigned int(32bit) to unsigned long long (64bit)

In the code below, I multiplied 0xffffffff by 2 for an unsigned int(32bit) and stored it in a unsigned long long(64bit). Why don't I get the actual output which is 8589934588. Instead I get 4294967294.
Thanks in advance.
OUTPUT:
Sizeof i=4
Sizeof J=8
2xi=4294967292
/* Code starts here */
#include
#...

3

votes

2

answer

211

Views

### Do the modern compilers optimize unsigned int use in for loops?

Consider the following code:
for(unsigned i = 0; i < counter1; i++) {
for(unsigned j = 0; j < counter2; j++) {
// some code here
}
}
Are there any benefits of using unsigned int instead of just int in this context? Would a modern compiler optimize it somehow or is the only benefit just the larger si...

6

votes

1

answer

834

Views

### 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, fkey, ...) VALUES (4294967295, 3456, ...
However, looping ov...

2

votes

3

answer

1.5k

Views

### java: fastest way of converting a bye array byte [1000] to array int[500]

The goal is to convert every pair of bytes as a single unsigned 16bit int.
In C I would define an array[500] of 16bit unsinged int pointer and would point it the the array of bytes, but in java I am not aware of such short cut.
I know in Java there is no 16bit data type except char, however that is...

2

votes

3

answer

1.1k

Views

### How to detect overflow of “unsigned” long multiplication in Java?

Java does not have "unsigned" long values, of course, but sometimes signed long values are effectively treated as unsigned long values (the result of System.nanoTime(), for example). In this sense, arithmetic overflow doesn't so much mean overflow of a value as much as it means overflow of the 64-bi...