615 questions
Advice
1
vote
9
replies
206
views
Is this array subscripting behavior really undefined in C?
unsigned int n = 1;
char* s = "X" + 1;
s[-n];
Is that third statement undefined in C23? It seems it is, as by the standard in 6.5.2.1.2:
... The definition of the subscript operator [] is ...
0
votes
1
answer
98
views
Why does int in my DP array cause overflow in Coin Change II problem?
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<unsigned int > dp(amount+1, 0);
dp[0]=1;
for (int value: coins){
...
-1
votes
1
answer
53
views
Is there a way to make Python interpret `~3` as an unsigned integer?
Is there a way to make Python interpret ~3 as an unsigned integer?
0
votes
1
answer
46
views
Understanding SetArgPointee behavior, and conversion errors
I have a class Bar, with a reference of a class Foo which have a read method. I'm trying to unit test, using gtest, the behavior of the public method Bar::test(), which does something different based ...
0
votes
1
answer
138
views
Intentional Overflow [duplicate]
I'm wondering if it is allowed in C++ to use overflow on purpose.
In specific I want to increase a counter every cycle and when uint32_max is reached start from 0.
In my mind the easy way would be:
...
3
votes
2
answers
266
views
Negative value forced zero when assigned to uint16_t variable in C
I understand that unsigned and signed integers are just a different representation of the underlying bits according to two's complement. That is, the following is my observation -- b is a non-zero ...
0
votes
2
answers
98
views
C Unisgned Short is output correctly with %d but Unsigned Long is not unless using %u: 2-1 K&R C Book
after testing for 2-1 Exercise I found it interesting that the output from printing the value of the unsigned long integer with %d was not correct while using %u it was - the book did not mention %u ...
0
votes
1
answer
127
views
Unsigned int weird behavior with Loops in C
In C, I encounter this weird problem:
For the following code:
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
unsigned int i = 0;
for (int j = i - 1; j < ...
0
votes
0
answers
52
views
How can I convert a string to "unix mode bits" u32 in rust [duplicate]
I am trying to convert a string value (taken from environment variables) to unix mode bits used in the std::fs::Permissions::from_mode() method. I haven't found much on the subject and am hoping this ...
1
vote
2
answers
204
views
Is 0u defaulting to a signed int?
The following code triggers a signed unsigned comparison warning:
uint16 x = 5;
if(((x) & (uint16)0x0001u) > 0u) {...}
Replacing the if with this eliminates the warning:
if(((x) & (uint16)...
2
votes
1
answer
120
views
Why does input exceeding unsigned int cause data loss, but not for size_t on a 64-bit system?
I'm working with C, and I've noticed some interesting behavior when handling large integer inputs using scanf. Specifically, when I input a number larger than the maximum value that can be stored in ...
1
vote
2
answers
119
views
C++ reading unsigned int values from byte buffer
After more than 20 years off, I started to get back into coding again, and started re-learning C++ (a language I practically never used, I used to be an Object Pascal man). So, I consider myself a ...
-1
votes
1
answer
145
views
uint_MAX wrap around: can I trust it for ringbuffer head and tail?
I have a ringbuffer that stores head and tail index as an unsigned integer value. According to this source it is enough to only wrap at retrieval of the indices and just let the uint behaviour take ...
0
votes
1
answer
428
views
Is the rule for unsigned integer overflow, the carry in matches the carry out?
I'm reading a textbook on systems programming and it states that an overflow occurs for unsigned integers if and only if the carry-in bit is mismatched with the carry-out, in the left-most bit.
So if ...
3
votes
2
answers
146
views
Is there a way to make the compiler raise an error instead of allowing unsigned int wrapping?
Disclaimer: I am a C++ and programming noob
I was making a Profile class as an exercise (from the book I am reading) which would hold the names and ages of profiles/people. The invariant I defined for ...
0
votes
0
answers
71
views
How to manipulate bit of JavaScript integer number as a 64 bit unsigned int [duplicate]
I solve problem using JavaScript in leetcode. I try to solve problem no : 2939. I already solve it. My solution is going fails when test case has 64 bit integer number. So I try to find out why it ...
0
votes
3
answers
91
views
Is there a general approach for optimizing bitwise expressions that distinguish two arbitrary sets of integers?
For context, I need to write a test for an integer between 0 and 7 inclusive, that evaluates to true for {1,3,4,6} and false for {0,2,5,7}. I thought for a couple minutes about whether there might be ...
-1
votes
2
answers
210
views
Why does "left shift count >= width of type" seemingly kick in late?
For context, I on a 64-bit (AMD64) little-endian system and am using g++ -std=gnu++23 -O3 -Wall to compile my code, if it makes any difference.
The following code snippet all shows some seemingly ...
1
vote
0
answers
78
views
Creating C++ templates with a value switch based on typename
I have created a set of templates intended for unsigned integer types (e.g. uint#_t where # = 8, 16, 32, or 64).
Most of the template functions are generic. The exact same steps are carried out each ...
3
votes
1
answer
469
views
64 unsigned integers in Lua 5.3/5.4 do not behave like in "Programming in Lua"
I'm reading "Programming in Lua, Forth Edition", and I'm stuck on exercise 13.1, because Lua does not seem to behave like in the book. The section 13.2, about Unsigned Integers, states:
> ...
1
vote
1
answer
71
views
How would I store multiple numbers in a UInt16Array or UInt32Array?
If I have one small number, that can be from 0-8, and another larger number that can be from 0-50, how would I store both of these numbers in one index of a UInt16Array or UInt32Array?
I want to be ...
4
votes
4
answers
654
views
How to loop over 0 .. v.len() - 1 when v might be empty?
What's the correct way to loop over 0 .. v.len() - 1 when the vector v might be empty?
Consider the following code, which prints all choices of 2 elements in a vector [5, 6, 7]:
fn main() {
let v: ...
1
vote
3
answers
220
views
Safe countdown loop
The following code will produce a segfault due to integer overflow when i reaches -1. If I change "unsigned int i" to "char i", then it will work fine but will generate a compiler ...
22
votes
7
answers
3k
views
Emulating signed integers using unsigned integers in C
In Jens Gustedt's book Modern C, on page 59, he explains how signed integers can be emulated using unsigned integers.
His example code shows how one can implement a comparison of two unsigned integers ...
0
votes
2
answers
2k
views
How to calculate the absolute difference of two unsigned integers?
How can one prevent overflow when calculating the absolute of the difference of two unsigned integers?
The result must be an unsigned integer as well (actually independent of the processer, as it will ...
-3
votes
2
answers
106
views
Why is the expected output not displayed [duplicate]
why there is no output for this?
#include <stdio.h>
int main() {
int i;
int array[4] = {10, 25, 36, 42};
// int size=sizeof(array) / sizeof(int);
for (i = -1; i < sizeof(...
2
votes
2
answers
224
views
What happens when you pass a signed integer to a function that expects an unsigned integer for the purpose of filtering/mixing?
I need to resize some images and the library I'm using is stb, the free header-only C library, and it has functions for uint8, uint16, uint32, and float. I have some images that are signed int16, with ...
7
votes
1
answer
173
views
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 ...
2
votes
2
answers
115
views
What are the 5bits and the 6bits in when bit shifting by the size of the integer?
I'm referring to this article specifically this part
Oversized Shift Amounts: Shifting a uint32_t by 32 or more bits is undefined. My guess is that this originated because the underlying shift ...
1
vote
1
answer
284
views
Fixed Point Binary Multiplication Over Flow Problem
I am doing 32bit unsigned multiplication of two fixed point integers like below:
888.88 x 805.00 = 7,155,484,000 (greater then 32 bit)
But i need the result like below:
888.88 x 805.00 = 715,548.4
I ...
3
votes
2
answers
184
views
An unsigned int literal outside bounds
If I define a variable of the unsigned int type and initialize it with a value outside its bounds, it uses modulo and assigns itself a value in its range, right?
For example:
unsigned int a = ...
1
vote
4
answers
332
views
Branchless way to add two UINT while avoiding overflow?
I'm curious if there's a branchless way to do this, or perhaps just a generally better way:
uint16_t add_with_no_overflow(uint16_t num, uint16_t delta)
{
if (UINT16_MAX - delta < num)
{
...
-5
votes
1
answer
83
views
Extract from array to integer [closed]
How to extract the number 0145525000 from the array 0x00, 0x50, 0x52, 0x45, 0x01 in C language?
#include <stdio.h>
int main()
{
uint8_t arr[] = {0x00, 0x50, 0x52, 0x45, 0x01};
...
2
votes
2
answers
146
views
Strange result of using T1 = unsigned T2 in C++
The following code confuses me.
https://godbolt.org/z/WcMTYM1q7
#include <iostream>
using ll = long long;
using ull = unsigned ll;
int main() {
ull x = 0;
std::cout << x << ...
1
vote
2
answers
172
views
Storing deltas for Unsigned Longs
I am writing a class that has a bunch of metric properties of ulong data type
class Metrics {
public ulong Memory
public ulong Handles
public ulong Calls
}
The reason I use ulong is because it'...
0
votes
1
answer
209
views
rust relative array indexing
I'm a fairly advanced programmer (experience in Java, Python, C#, C, C++) who is now trying to learn Rust for the first time.
Rust is quite different from any language I've tried before, so I'm ...
0
votes
1
answer
166
views
Problem with printing unsigned long integers
I have the following simple program that reads in a number given as a string and prints it. It works for small numbers, but when I try to use numbers of size unsigned long, like "...
1
vote
1
answer
192
views
How to modify template type to be unsigned?
template<class T>
void foo(T s)
{
unsigned T x = -1;
//...
}
{
int x = 129;
foo(x);
}
How can I modify template type to be unsigned?
I know that this is bad practice due to the ...
0
votes
1
answer
497
views
numpy - casting uint32 to uint8
I have the following code in python using numpy:
import numpy
a = 423
b = numpy.uint8(a)
print(b)
It gives me the result:
b = 167
I understand that uint8 (unsigned 8-bit integer) can represent values ...
0
votes
1
answer
113
views
Why `-1 + unsigned(0) > 0` in C++? [duplicate]
The following code output -1 1 in C++.
If i is evaluated as -1, why is it larger than 0?
#include <stdio.h>
int main() {
auto i = -1 + unsigned(0);
printf("%d %d\n", i, i > 0);
...
-1
votes
1
answer
84
views
I want to convert integer into an array in C and this is my code and it kind of works
Im learning C right now and I've got this problem I want to convert the integer into an array my code works but the problem is that I declare the size of an array in the beginning and I want to make ...
0
votes
1
answer
238
views
How does masking work with int/Integer in java?
Since bytes are signed in java, in order to obtain unsigned integer values we mask with 0xFF asper:
byte signed = -1;// -—> 0b11111111
int unsigned = signed & 0xFF;// —-> 0b11111111 & ...
0
votes
1
answer
951
views
Multiply with overflow: Rust Runtime error
In the Rust program, I'm trying to write I'm required to multiply 2 u64s and count the number of 1s when the product is represented as binary.
// Intentionally left uninitialized
let some_u64: u64;
...
-1
votes
3
answers
343
views
unsigned integer devision rounding AVR GCC
I am struggling to understand how to divide an unsigned integer by a factor of 10 accounting for rounding like a float would round.
uint16_t val = 331 / 10; // two decimal to one decimal places 3.31 ...
2
votes
2
answers
56
views
My find-biggest-number Recursive function returns a discremented value
I have a problem in this recursive function that basically takes two numbers and returns the biggest one of them without using comparison (> || < ) operators, thing is, it returns dicremented ...
1
vote
1
answer
587
views
How do I convert Data to signed Int?
I'm in an iOS project where we convert incoming Data to a byteArray. It looks something like this:
class ViewController: UIViewController {
let data: Data? = nil
override func viewDidLoad(...
3
votes
3
answers
240
views
Questions about C strlen function
I tried to compare with strlen(string) with -1 but different methods gave different results:
char string[] = {"1234"};
int len = strlen(string);
int bool;
bool = -1 < strlen(string);
...
2
votes
2
answers
101
views
inconsistent results with left shift operator (<<)
I am learning bit manipulation using C. I encountered a problem when writing a program that converts a binary to decimal, particularly in the for loop of the program. The following is my code:
...
0
votes
0
answers
223
views
Sprintf function converting int to a single char instead of a string
I'm trying to convert the unsigned long integer converted_binary, which contains 10000000000 to a string, but sprintf converts it to a single character 1 instead.
I am able to know this through the ...
0
votes
1
answer
2k
views
Why is image.astype(uint8) different from np.clip(image.astype(uint32), 0, 255) in Python?
I am a bit confused about a behavior of my code.
I have an image tensor with values in range [0, 255] to which I have added some Gaussian noise so that the resulting tensor has values in larger and ...