programming.dev
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
andioop to Programming HorrorEnglish ·
edit-2
1 year ago

God I wish there was an easier way to do this

message-square
55
link
fedilink
195

God I wish there was an easier way to do this

andioop to Programming HorrorEnglish ·
edit-2
1 year ago
message-square
55
link
fedilink
  • Mac
    link
    fedilink
    arrow-up
    23
    ·
    edit-2
    1 year ago

    who needs modulo when you can get less characters out of

    while (number > 1) {
      number -= 2;
    }
    return number;
    

    very efficient

    edit: or theres the trusty iseven api

    • NullPointer
      link
      fedilink
      arrow-up
      9
      ·
      1 year ago

      here is somewhat less:

      return (number % 2) == 0;

      • pivot_root@lemmy.world
        link
        fedilink
        arrow-up
        10
        ·
        1 year ago

        return !(number & 1);

        • MuchPineapples@lemmy.world
          link
          fedilink
          arrow-up
          8
          ·
          edit-2
          1 year ago

          This is the way. Modulo takes too long to compute, bitwise compare should be a lot faster.

          return !(number & 0x1);
          
          • recursive_recursion [they/them]
            link
            fedilink
            English
            arrow-up
            5
            ·
            edit-2
            1 year ago

            oh shit yo

            this comment chain is pretty awesome, I learned a lot from this thanks!

      • huf [he/him]@hexbear.net
        link
        fedilink
        English
        arrow-up
        4
        ·
        1 year ago

        just check the last bit jesus christ, what is it with these expensive modulo operations?!

        return !(n&1);

    • perviouslyiner@lemm.ee
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 year ago

      are the negative numbers all even?

      • I_am_10_squirrels@beehaw.org
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        Yes

Programming Horror

programming_horror

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there’s also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @[email protected]

Rules

  • Keep content in english
  • No advertisements (this includes both code in advertisements and advertisement in posts)
  • No generated code (a person has to have made it)

Credits

  • Icon base made by Lorc under CC BY 3.0 with modifications to add a gradient
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 1 user / day
  • 1 user / week
  • 3 users / month
  • 49 users / 6 months
  • 761 local subscribers
  • 2.09K subscribers
  • 36 Posts
  • 296 Comments
  • Modlog
  • mods:
  • Vacant
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org