Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Overflow, carry, etc i C#

Overflow, carry, etc i C#

Scheduled Pinned Locked Moved C#
csharpperformancequestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hanzibal2
    wrote on last edited by
    #1

    Hi all! Is there a way of checking overflow, carry, etc on integer arithmetic in C#? I'm an old assembler programmer and I sometimes would like this funtionality when performing bitwise operations on integer variables. Also, I would like to see ROL and ROR (rotate left, rotate right). Do these exist in C#? For performance reasons, implementing a class realizing this is not an option. Thank you.

    L P 2 Replies Last reply
    0
    • H hanzibal2

      Hi all! Is there a way of checking overflow, carry, etc on integer arithmetic in C#? I'm an old assembler programmer and I sometimes would like this funtionality when performing bitwise operations on integer variables. Also, I would like to see ROL and ROR (rotate left, rotate right). Do these exist in C#? For performance reasons, implementing a class realizing this is not an option. Thank you.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See Arithmetic Exceptions[^]. ROL and ROR are not part of the language, you would need to write your own versions.

      hanzibal2 wrote:

      For performance reasons, implementing a class realizing this is not an option.

      If you are worried about performance then you should not be using C#.

      Use the best guess

      J H 2 Replies Last reply
      0
      • L Lost User

        See Arithmetic Exceptions[^]. ROL and ROR are not part of the language, you would need to write your own versions.

        hanzibal2 wrote:

        For performance reasons, implementing a class realizing this is not an option.

        If you are worried about performance then you should not be using C#.

        Use the best guess

        J Offline
        J Offline
        Jonathan Nethercott
        wrote on last edited by
        #3

        Quote:

        ROL and ROR are not part of the language

        Although << and >> (as well as <<= and >>=) are equivalent to SHL and SHR if I remember my aseembly language correctly...

        Jon CodeWrite

        L 1 Reply Last reply
        0
        • J Jonathan Nethercott

          Quote:

          ROL and ROR are not part of the language

          Although << and >> (as well as <<= and >>=) are equivalent to SHL and SHR if I remember my aseembly language correctly...

          Jon CodeWrite

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You are, of course, correct. I have only once or twice in 20+ years of C/C++/C# programming felt the need for rotation shift operators. But in my assembler days I probably used them all the time.

          Use the best guess

          1 Reply Last reply
          0
          • L Lost User

            See Arithmetic Exceptions[^]. ROL and ROR are not part of the language, you would need to write your own versions.

            hanzibal2 wrote:

            For performance reasons, implementing a class realizing this is not an option.

            If you are worried about performance then you should not be using C#.

            Use the best guess

            H Offline
            H Offline
            hanzibal2
            wrote on last edited by
            #5

            Thank you for your answer. There are so many advantages with C# over almost any other language that I just can't live without and since C# is the language I chose for my project, why not write repetitive sections as effective as possible? You are correct, but you answered a question that I didn't ask ;-)

            L 1 Reply Last reply
            0
            • H hanzibal2

              Thank you for your answer. There are so many advantages with C# over almost any other language that I just can't live without and since C# is the language I chose for my project, why not write repetitive sections as effective as possible? You are correct, but you answered a question that I didn't ask ;-)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              hanzibal2 wrote:

              You are correct, but you answered a question that I didn't ask

              Where exactly did I do that?

              Use the best guess

              1 Reply Last reply
              0
              • H hanzibal2

                Hi all! Is there a way of checking overflow, carry, etc on integer arithmetic in C#? I'm an old assembler programmer and I sometimes would like this funtionality when performing bitwise operations on integer variables. Also, I would like to see ROL and ROR (rotate left, rotate right). Do these exist in C#? For performance reasons, implementing a class realizing this is not an option. Thank you.

                P Offline
                P Offline
                parths
                wrote on last edited by
                #7

                Did you consider the checked[^] keyword?

                "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman "Philosophy is a study that lets us be unhappy more intelligently." -Anon.

                H 1 Reply Last reply
                0
                • P parths

                  Did you consider the checked[^] keyword?

                  "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman "Philosophy is a study that lets us be unhappy more intelligently." -Anon.

                  H Offline
                  H Offline
                  hanzibal2
                  wrote on last edited by
                  #8

                  Great, that's exactly what I was looking for. Thanks!

                  L 1 Reply Last reply
                  0
                  • H hanzibal2

                    Great, that's exactly what I was looking for. Thanks!

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    checked is horribly slow. If you're working with the carry as a performance optimization, you're better off calculating it manually. See Hacker's Delight chapter 2.12, Overflow Detection.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups