Skip to main content
Filter by
Sorted by
Tagged with
Advice
1 vote
9 replies
206 views

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 ...
Kyle's user avatar
  • 1,116
0 votes
1 answer
98 views

class Solution { public: int change(int amount, vector<int>& coins) { vector<unsigned int > dp(amount+1, 0); dp[0]=1; for (int value: coins){ ...
OXEN's user avatar
  • 13
-1 votes
1 answer
53 views

Is there a way to make Python interpret ~3 as an unsigned integer?
Geremia's user avatar
  • 5,844
0 votes
1 answer
46 views

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 ...
LPo's user avatar
  • 95
0 votes
1 answer
138 views

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: ...
genicki's user avatar
  • 83
3 votes
2 answers
266 views

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 ...
fschwaiger's user avatar
0 votes
2 answers
98 views

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 ...
Giorgio Vitanza's user avatar
0 votes
1 answer
127 views

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 < ...
William Li's user avatar
0 votes
0 answers
52 views

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 ...
Marcus Ruddick's user avatar
1 vote
2 answers
204 views

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)...
Johan's user avatar
  • 13
2 votes
1 answer
120 views

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 ...
Nalan PandiKumar's user avatar
1 vote
2 answers
119 views

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 ...
AlexisVanWien's user avatar
-1 votes
1 answer
145 views

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 ...
glades's user avatar
  • 5,392
0 votes
1 answer
428 views

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 ...
Addem's user avatar
  • 4,013
3 votes
2 answers
146 views

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 ...
a_floating_point's user avatar
0 votes
0 answers
71 views

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 ...
Md Tazri's user avatar
0 votes
3 answers
91 views

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 ...
gbromios's user avatar
  • 472
-1 votes
2 answers
210 views

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 ...
Mel's user avatar
  • 127
1 vote
0 answers
78 views

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 ...
Trashman's user avatar
  • 1,664
3 votes
1 answer
469 views

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: > ...
Sebastien Diot's user avatar
1 vote
1 answer
71 views

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 ...
radio's user avatar
  • 103
4 votes
4 answers
654 views

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: ...
Dmitry's user avatar
  • 340
1 vote
3 answers
220 views

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 ...
Amati's user avatar
  • 1,542
22 votes
7 answers
3k views

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 ...
JezuzStardust's user avatar
0 votes
2 answers
2k views

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 ...
pas-calc's user avatar
  • 170
-3 votes
2 answers
106 views

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(...
aleena george's user avatar
2 votes
2 answers
224 views

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 ...
Zebrafish's user avatar
  • 16.5k
7 votes
1 answer
173 views

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 ...
dscerutti's user avatar
  • 333
2 votes
2 answers
115 views

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 ...
Enlico's user avatar
  • 30.3k
1 vote
1 answer
284 views

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 ...
Adil Ahmed's user avatar
3 votes
2 answers
184 views

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 = ...
Rajdeep Sindhu's user avatar
1 vote
4 answers
332 views

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) { ...
Jonathan Levin's user avatar
-5 votes
1 answer
83 views

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}; ...
Сергей Т's user avatar
2 votes
2 answers
146 views

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 << ...
earthmessenger's user avatar
1 vote
2 answers
172 views

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'...
totalZero's user avatar
  • 345
0 votes
1 answer
209 views

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 ...
Matthias Verstraete's user avatar
0 votes
1 answer
166 views

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 "...
3nondatur's user avatar
  • 475
1 vote
1 answer
192 views

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 ...
AnnikGet's user avatar
0 votes
1 answer
497 views

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 ...
jirikadlec2's user avatar
  • 1,276
0 votes
1 answer
113 views

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); ...
Max's user avatar
  • 473
-1 votes
1 answer
84 views

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 ...
Giorgi's user avatar
  • 13
0 votes
1 answer
238 views

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 & ...
linker's user avatar
  • 899
0 votes
1 answer
951 views

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; ...
mich abay's user avatar
-1 votes
3 answers
343 views

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 ...
Jay Dee's user avatar
  • 230
2 votes
2 answers
56 views

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 ...
Inferno's user avatar
  • 47
1 vote
1 answer
587 views

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(...
Joakim Sjöstedt's user avatar
3 votes
3 answers
240 views

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); ...
chaganhu's user avatar
2 votes
2 answers
101 views

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: ...
user avatar
0 votes
0 answers
223 views

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 ...
Olamide Olanrewaju's user avatar
0 votes
1 answer
2k views

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 ...
Jonas G.'s user avatar
  • 159

1
2 3 4 5
13