Why Does Everyone Keep Telling Me to Use C++ -- The Increment Operator Confusion
Description
A screenshot from Reddit's r/programminghorror subreddit showing a post titled 'Why does everyone keep telling me to use c++?' submitted by user ZERICO2005. The post describes a task to create a C function that finds the rightmost 0 in an integer, flips it to 1, and flips all 1's to its right to 0's. The code shows a uint32_t function using bitwise operations (AND, OR, NOT, left shift, right shift) with detailed comments. The function uses bit manipulation with operators like &, |=, ~, <<=, and >>=. The punchline at the bottom reads: 'Why are people so adamant that I use c++ instead of C?' -- the joke being that people are telling the author to 'use C++' meaning the language, but the author's C code already heavily uses the ++ increment operator, making them oblivious to the pun
Comments
39Comment deleted
The real horror isn't the code -- it's that this person wrote a perfectly correct bit manipulation function in C and still can't figure out why everyone keeps recommending the language named after the operator they're already using on every line
This code is a great interview question to see if a candidate will write 20 lines of buggy code or just give you `x | (x + 1)` and go to lunch
i get it but the joke is built on false premise return c+1 is not the solution to the original problem neither can the "flipping" be done by just adding something to the c Comment deleted
not a "false premise" that's how incrementing in base-2 works --- a chunk of least significant ones is flipped to zeroes and the next bit (the first zero) is flipped to one (if "right-most" means "least significant", and 1's and 0's are assumed to be the bits of input) Comment deleted
damn I misread this I read it as "to the left" Comment deleted
but what about endians? Comment deleted
endianness applies to bytes within integers, there are zero mentions of bytes anywhere, make your own conclusions Comment deleted
how then would a +1 flip digits? Comment deleted
math is cool like that Comment deleted
hint: 149375933999999999 + 1 = 149375934000000000, now apply the same thing to binary Comment deleted
JavaScript would disagree with you Comment deleted
Wait nvm ccording to JavaScript 149375933999999999 is already 149375934000000000 Comment deleted
no Comment deleted
you are flipping 99's not 1's Comment deleted
because it's base 10 and not base 2 Comment deleted
hint: mayhaps the digit 9 might be magical because we're working in base 10 Comment deleted
if your LSB is on the different side — then +1 will flip from the other side but the task clearly defines left and right. not LSB and MSB Comment deleted
"left" always means "MSB" and "right" always means "LSB" because that's how we humans represent integers, hope this helps Comment deleted
not Comment deleted
That's arabic numbers. (Not the system arabs actually use) Comment deleted
true Comment deleted
Roman numbers are even weirder there they relative position can invert its value Comment deleted
shit. I may have some memory bug if you are all don't agree Comment deleted
@Capstra, 🖕 Comment deleted
Roman numerals: exist Comment deleted
if you have a different order of bytes — then corresponding bits are reordered with them together Comment deleted
that's not how it works Comment deleted
Big endian is dying or dead Comment deleted
It doesn’t matter Comment deleted
Does bitshift expose endianness? No. Does math expose it? No. Does most and oeast significant bit depend on big or little endian numbers? No. Then why should it matter here? Little endianness is only relevant when not working with whole numbers. A.K.A. streaming or storing them in a non uniform register/slot/buffer. Comment deleted
std::uint32_t func(std::uint32_t c) { if (c == -1u) return 0; auto idx = std::countr_zero(~c); auto mask = idx == 0 ? -1u : (1u << (idx + 1u)) - 1u; return c ^ mask; } 🤡 <- it's me Comment deleted
A programming meme on my vibecoding channel?? You expect me to comprehend actual C?? Comment deleted
This made laugh, thanks ❤️ Comment deleted
You just need vibe understanding 🙌 Comment deleted
Why the fuck you need that much code for this? Comment deleted
I am pretty sure you can solve it with one single while/for loop and a bool Comment deleted
you solve this with c++ Comment deleted
the meme is that all what you need is c++ 🌚 Comment deleted
Not understanding this made me feel like shit. Should’ve outsourced my brain to ChatGPT Comment deleted