# Questions tagged [unsigned-integer]

220 questions

1

votes

1

answer

125

Views

### How to implement uint32 addition and subtraction operations in Ruby?

Please do not suggest using any gems or creating classes for unsigned integers.
Input data - strings that represent 4 byte sequence.
Output data should be the same type of string.
This is what I've got so far:
def addition(augend, addend)
sum = augend.to_i(16)+addend.to_i(16)
sum -= 2**32 if sum >=...

0

votes

0

answer

31

Views

### How to fix qsort overwriting values, and why this happens?

When I run qsort() in C, its changes some values.
For example I have to sort this array by the left column increasing order:
initial vecinos array:
1 ------ 2
2 ------ 1
2 ------ 4
4 ------ 2
2 ------ 3
3 ------ 2
3 ------ 4
4 ------ 3
expected vecinos array:
1 ------ 2
2 ----...

1

votes

5

answer

114

Views

### Signed integer and unsigned integer [duplicate]

This question already has an answer here:
Why Do We have unsigned and signed int type in C?
4 answers
I am studying c language and there are two different integer types, signed/unsigned.
Signed integers can present both positive and negative numbers. Why do we
need unsigned integers then?

1

votes

1

answer

915

Views

### How to declare ULARGE_INTEGER in c#?

Based upon this question How to declarate LARGE_INTEGER in C# with answer of:
[StructLayout(LayoutKind.Absolute, Size=8)]
struct LARGE_INTEGER
{
[FieldOffset(0)]public Int64 QuadPart;
[FieldOffset(0)]public UInt32 LowPart;
[FieldOffset(4)]public Int32 HighPart;
}
Is my assumption below for declaring...

1

votes

2

answer

1.1k

Views

### How to shift int64 value to unsigned int64 space without changing the order?

For example:
INT64_MIN to 0
0 to INT64_MAX+1
INT64_MAX to UINT64_MAX
Below is my code:
uint64_t int64ToUint64(int64_t value)
{
uint64_t uvalue = value - std::numeric_limits::min();
return uvalue;
}
It works, but there is signed integer overflow, and per C++ standard, signed integer overflow is beha...

1

votes

1

answer

2.1k

Views

### 12bit signed value handling in 16 bit variable

I have 12bit signed value. What is the correct way to store this 12bit signed value in 16bit variable? MSB bit has to be moved to 15th bit position?
converted_Temperature = (Tmp_value & (1

1

votes

2

answer

1.3k

Views

### qbytearray.at() to unsigned int

Ok I have a two part question about converting a element int a QByteArray (should be a char right?) to an unsigned integer.
Here is my code:
QByteArray data_read_buffer;
unsigned int data_recieved_size = 0;
/* the code below is an external function that populates the QbyteArray. shouldn't be import...

1

votes

1

answer

135

Views

### jOOQ Function generation of UNSIGNED BIGINT generates LONG rather than ULONG

I have a SQL (MySQL) function defined as:
FUNCTION `ifs_getPrimaryAddress`(aEntityId BIGINT unsigned, aTable VARCHAR(25), aResultType VARCHAR(10)) RETURNS varchar(500)
Note the UNSIGNED bigint argument. When I generate the jOOQ, it creates the argument as a Long rather than a ULong:
public static...

1

votes

1

answer

88

Views

### logical right shift in numba

The following code
@jit(uint32(uint32))
def foo(x):
return (x >> 6)
print(hex(foo(0xffffffff)))
outputs
0xffffffff
because numba uses arithmetic right shift despite the fact that the operand is defined as unsigned.
Is there any way to convey it to use the logical shift?
Actually, from the answer...

1

votes

1

answer

89

Views

### function returning vector in matlab

I have a function returning a vector in MatLab, the problem is that function should return a unsigned integer value, I've debugged my code and I realize that the variable galois_value turns into a vector when I do a bitand and a bitxor operation. I've tried to do a typecast to turn the vector into a...

1

votes

2

answer

111

Views

### Using printf() function in PHP to print negative integer with unsigned format specifier

I have the following code:
Output:
%u = 4294967284
I am unable to understand the reason behind such output.

1

votes

1

answer

53

Views

### Architecture agnostic code

I have this piece of code where the aim is to determine if the packet received is valid or not:
size_t offset_to_a, offset_to_b;
u_int32_t file_name_length;
u_int32_t packet_length;
if ((offset_to_a + offset_to_b + file_name_length) > packet_length) {
// Invalid packet
}
size_t is u_int64_t perhaps...

1

votes

1

answer

61

Views

### Issue understanding an aspect of data allocation and unsigned ints

I have an assignment asking me to write a spell-checker by implementing a few functions into some given code. I am unable to compile due to some syntax errors.
First is:
speller.c:291:19: error: comparison of array 'trav_ptr->word' not equal to a null
pointer is always true [-Werror,-Wtautological-p...

1

votes

1

answer

0

Views

### Mixing function for non power of 2 integer intervals

I'm looking for a mixing function that given an integer from an interval

1

votes

2

answer

0

Views

### Convert a .NET long to an unsigned 8-byte integer

For the purposes of setting a value in Active Directory I would like to convert a long to an unsigned 8-byte integer, for assignment to an AD property.
How can I do this?

1

votes

2

answer

0

Views

### How can I get a byte that represents an unsigned int in Java?

I have integers from 0 to 255, and I need to pass them along to an OutputStream encoded as unsigned bytes. I've tried to convert using a mask like so, but if i=1, the other end of my stream (a serial device expecting uint8_t) thinks I've sent an unsigned integer = 6.
OutputStream out;
public void wr...

1

votes

1

answer

0

Views

### Translate embedded C function to PHP function

I'm trying to 'translate' the following function written in C in PHP. The problem is, the results returned by the function written in PHP are different from those returned by the one in C.
I think the problem comes from integer overflows in PHP which is not able to treat variables as unsigned 32-bit...

1

votes

2

answer

308

Views

### Wrong result in operation with unsigned integers

I have a problem with an arithmetic operation with unsigned integer variables.
All the variables are defined as uint32_t.
This is the arithmetic operation:
batt += (uint32_t) ((((charg - discharg) * (time_now - time_old)) / 1000) + 0.5);
The values before the operation are:
batt = 8999824
charg = 21...

1

votes

3

answer

6.9k

Views

### Proper way of converting C's uint32 to int in java

I am building a project which consists of multiple components and these components communicate with each other in byte arrays using sockets. The problem is one component is written in C and supports Unsigned primitive types while other components are written in Java and does not support Unsigned typ...

1

votes

4

answer

961

Views

### Comparison size_t variable with -1 (maximum size value) in c++ code

I'm refactoring a library and trying to get rid of many gcc warnings. The big part of these warning are about signed / unsigned comparison and are related to the usage of size_t. The library works on 64 bit Linux systems.
A programmer used -1 as the special value similar to std::string::npos.
There...

1

votes

1

answer

68

Views

### “if an int can hold all values of the original type then the value is converted to int , else to unsigned int”--What it means?

The above line is about implicit integral promotions/conversions in C, taken from the book by Mike Banahan Section 2.8.1.1 (Link). Here's what the exact paragraph is:
No arithmetic is done by C at a precision shorter than int, so these conversions are implied almost whenever you use one of the objec...

1

votes

3

answer

675

Views

### Inserting unsigned integer value into bytebuffer, maintaining binary representation

I'm trying to put the following binary representation into a bytebuffer for 4 bytes. But since Java doesn't do unsigned, I'm having trouble: 11111111000000001111111100000000
ByteBuffer bb = ByteBuffer.allocate(8);
bb.putInt(Integer.parseInt('11111111000000001111111100000000', 2));
//throws numberfor...

1

votes

1

answer

8.4k

Views

### Declaring a long long array with unsigned int size

I was doing a problem when I encountered a segmentation fault while declaring an array as:
long long ways[max+1];
where,
unsigned int max = findMax(l,T); // l is an unsigned int array and T is an int.
and findMax is a function of the type:
unsigned int findMax(unsigned int arr[],int size)...

1

votes

1

answer

2.7k

Views

### printf not printing unsigned int

I'm experiencing the following problem printing unsigned numbers .
Here's whats happening:
When I input a negative value in the array and then try to print it, I can't print the number but other amount is printed.
int cant;
int a[30][30];
int printequation (){
int x,y;
for (x=0;x

1

votes

2

answer

132

Views

### Confused about the value of a number and the range of the number in C

I missed my lecture and I don't understand the last two points. Can someone explain the third point and show me how to calculate the forth point? Thanks

1

votes

2

answer

677

Views

### Getting the high half and low half of a full integer multiply

I start with three values A,B,C (unsigned 32bit integer). And i have to obtain two values D,E (unsigned 32 bit integer also). Where
D = high(A*C);
E = low(A*C) + high(B*C);
I expect that multiply of two 32bit uint produce 64bit result. 'high' and 'low' is just my covnention for mark the first 32 bit...

1

votes

2

answer

84

Views

### Why DataInput interface in Java doesn't have readUnsignedInt method

Java does not support unsigned integer types and the easiest way to represent an unsigned integer in Java is using a larger integer type.
DataInput interface does provide methods for reading unsigned integer types - readUnsignedByte, which returns short and readUnsignedShort, which returns int.
Wh...

1

votes

1

answer

869

Views

### How to get '(unsigned int)' operation in R?

I would like to reproduce Version 2 of the following C code in R.
The problem is that R doesn't seem to have an operation equivalent to C's '(unsigned int)'
(there are no unsigned integers in R). I looked into some packages but couldn't easily
find it. To do the conversion manually (or with a functi...

1

votes

1

answer

88

Views

### 16 to 32 bit integer conversion vs performance

I want to load 16 bit unsigned integers from an array and use these values for 32 bit unsigned calculations in C++. I have the choice between storing the values as 16 bit array (less memory) or 32 bit array (more memory consumption).
My code should be compilable with common C++ compilers and run on...

1

votes

1

answer

164

Views

### Why compiler doesn't generate warnings for negative value with size_t? [duplicate]

This question already has an answer here:
What happens if I assign a negative value to an unsigned variable?
5 answers
In the following code, I have used size_t as an function argument and passed negative value. I have compiled program on GCC(Linux) using following command.
g++ -Wall size.cpp -o si...

1

votes

3

answer

134

Views

### 64-bit unsigned integers which cannot map onto a double [duplicate]

This question already has an answer here:
Are all integer values perfectly represented as doubles? [duplicate]
5 answers
Are there any 64-bit unsigned integer values which cannot be represented with a double-precision floating-point type? (As a double is also 64-bit wide there must be some.) If so,...

1

votes

3

answer

122

Views

### Unsigned 64 bit integers which cannot map onto a double [duplicate]

This question already has an answer here:
Are all integer values perfectly represented as doubles? [duplicate]
5 answers
Are there unsigned 64 bit integer values which cannot be represented with a double precision floating point type? (As a double is also 64 bit there must be some.) If so, how can...

1

votes

2

answer

6.5k

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

160

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

448

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

902

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

157

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...