Skip to content
DevMeme
6778 of 7590
Why Does Everyone Keep Telling Me to Use C++ -- The Increment Operator Confusion
Languages Post #7432 · source on Telegram

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

39
Anonymous ★ Top Pick 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
  1. Anonymous ★ Top Pick

    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

  2. Anonymous

    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

  3. @beton_kruglosu_totchno 9mo

    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

    1. @sylfn 9mo

      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)

      1. @beton_kruglosu_totchno 9mo

        damn I misread this I read it as "to the left"

      2. @deadgnom32 9mo

        but what about endians?

        1. @purplesyringa 9mo

          endianness applies to bytes within integers, there are zero mentions of bytes anywhere, make your own conclusions

          1. @deadgnom32 9mo

            how then would a +1 flip digits?

            1. @purplesyringa 9mo

              math is cool like that

              1. @purplesyringa 9mo

                hint: 149375933999999999 + 1 = 149375934000000000, now apply the same thing to binary

                1. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

                  JavaScript would disagree with you

                  1. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

                    Wait nvm ccording to JavaScript 149375933999999999 is already 149375934000000000

                2. @deadgnom32 9mo

                  no

                3. @deadgnom32 9mo

                  you are flipping 99's not 1's

                  1. @sylfn 9mo

                    because it's base 10 and not base 2

                  2. @purplesyringa 9mo

                    hint: mayhaps the digit 9 might be magical because we're working in base 10

                4. @deadgnom32 9mo

                  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

                  1. @purplesyringa 9mo

                    "left" always means "MSB" and "right" always means "LSB" because that's how we humans represent integers, hope this helps

                    1. @deadgnom32 9mo

                      not

                    2. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

                      That's arabic numbers. (Not the system arabs actually use)

                      1. @purplesyringa 9mo

                        true

                        1. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

                          Roman numbers are even weirder there they relative position can invert its value

                    3. @deadgnom32 9mo

                      shit. I may have some memory bug if you are all don't agree

                      1. @deadgnom32 9mo

                        @Capstra, 🖕

                    4. @chupasaurus 9mo

                      Roman numerals: exist

          2. @deadgnom32 9mo

            if you have a different order of bytes — then corresponding bits are reordered with them together

            1. @purplesyringa 9mo

              that's not how it works

        2. @Agent1378 9mo

          Big endian is dying or dead

        3. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

          It doesn’t matter

        4. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

          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.

  4. @VanuxaKR 9mo

    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

  5. @imfreetodowhatever 9mo

    A programming meme on my vibecoding channel?? You expect me to comprehend actual C??

    1. dev_meme 9mo

      This made laugh, thanks ❤️

    2. dev_meme 9mo

      You just need vibe understanding 🙌

  6. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

    Why the fuck you need that much code for this?

    1. @ZgGPuo8dZef58K6hxxGVj3Z2 9mo

      I am pretty sure you can solve it with one single while/for loop and a bool

      1. @q_rsqrt 9mo

        you solve this with c++

      2. dev_meme 9mo

        the meme is that all what you need is c++ 🌚

  7. @ajnart 9mo

    Not understanding this made me feel like shit. Should’ve outsourced my brain to ChatGPT

Use J and K for navigation