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

    Every bit aside for the ones bit is even. All you have to do is get the ones bit(the far right) for it being a 1 or 0. Which is the fastest and least amount of code needed.

    use bitwise &

    // n&1 is true, then odd, or !n&1 is true for even  
    
     return (!(n & 1));  
    
    • oatscoop
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      1 year ago

      I hate how lemmy turns “&” into “&” even in code blocks.