Thread: How can I improve/shorten my C code?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    19
    Quote Originally Posted by Hodor View Post
    a) Using bitwise OR the way you used it there is broken (it won't work... seriously)
    b) Yep, fair enough. I and many others find them annoying
    c) GOTO is not an ASM instruction. I take it that you haven't dabbled in ASM. Even if GOTO *was* an ASM instruction it's a branch
    Perhaps I'm mistaken, but it looks to me like goto is indeed an ASM instruction. I was always told to avoid goto in C because it was too "Assembly-ish".

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by C-UL8R View Post
    Perhaps I'm mistaken, but it looks to me like goto is indeed an ASM instruction. I was always told to avoid goto in C because it was too "Assembly-ish".

    It's just a branch. Writing in assembly the mnemonic has never (as far as I know) been "goto"; the example you give is a "C'ificiation"; it'll compile to an ASM branch instruction (which, yeah, are GOTOs but they're not called that in ASM)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hodor
    First, your use of bitwise OR (|) is dumb and incorrect. Second, even if it was correct it'd be obfuscated and that's not good.
    It is correct though since the result of the comparison operators is always 0 or 1, just semantically... unusual and hence obfuscated, and if someone thinks that they can freely replace || and && except for short-circuiting, they could get bitten when the operands aren't strictly 0 or 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Quote Originally Posted by laserlight View Post
    It is correct though since the result of the comparison operators is always 0 or 1, just semantically... unusual and hence obfuscated, and if someone thinks that they can freely replace || and && except for short-circuiting, they could get bitten when the operands aren't strictly 0 or 1.
    Yeah I only did it with comparison results but now that I know the compiler still manages to treat that as it would || & && I'll just use the originals

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by awsdert View Post
    You understand that 0 times any number equals 0 yes?
    You understand that 1 times any number equals the number that 1 was multiplied against yes?
    You understand that any comparison in C only results in 1 or 0 yes?
    You understand that bitwise OR just mangles the bits of 2 integers together (of which booleans are a part of) yes?
    Then you should easily understand what the code is doing just by looking at it,
    Hop over to the optimization challenge thread and show us your skills...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me shorten this code plz.
    By oblisgr in forum C Programming
    Replies: 3
    Last Post: 07-09-2020, 12:12 AM
  2. HOw can I shorten my code?
    By kdushyant297 in forum C Programming
    Replies: 1
    Last Post: 09-15-2017, 10:40 AM
  3. Making a function to shorten my code?
    By evilcubed in forum C Programming
    Replies: 8
    Last Post: 12-08-2012, 11:46 AM
  4. help to shorten my code.
    By hugoguan in forum C Programming
    Replies: 7
    Last Post: 12-01-2010, 02:19 AM
  5. Need to simplify/shorten this code. Help.
    By Lonck in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 04:23 AM

Tags for this Thread