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. (4/4)%2 = 1? wtf?

(4/4)%2 = 1? wtf?

Scheduled Pinned Locked Moved C#
visual-studioquestion
11 Posts 5 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.
  • J Offline
    J Offline
    jchalfant
    wrote on last edited by
    #1

    Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like: Convert((SomeColumn/4),'System.Int32')%2 = 1 Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.

    J L G L 4 Replies Last reply
    0
    • J jchalfant

      Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like: Convert((SomeColumn/4),'System.Int32')%2 = 1 Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.

      J Offline
      J Offline
      J 0
      wrote on last edited by
      #2

      jchalfant wrote:

      Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. 3/4 = .75

      Sound about right considering int values only hold whole numbers.

      1 Reply Last reply
      0
      • J jchalfant

        Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like: Convert((SomeColumn/4),'System.Int32')%2 = 1 Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        jchalfant wrote:

        I can't modulo a double by an int, thus forcing me to convert to int

        just convert the int to a double then... I am not sure I understand the problem. (4/4)%2 = 1 is correct. What are you trying to do?

        xacc.ide - now with IronScheme support
        IronScheme - 1.0 alpha 2 out now

        J 1 Reply Last reply
        0
        • L leppie

          jchalfant wrote:

          I can't modulo a double by an int, thus forcing me to convert to int

          just convert the int to a double then... I am not sure I understand the problem. (4/4)%2 = 1 is correct. What are you trying to do?

          xacc.ide - now with IronScheme support
          IronScheme - 1.0 alpha 2 out now

          J Offline
          J Offline
          jchalfant
          wrote on last edited by
          #4

          While C# can handle double % double, ADO.Net expressions can't. I've tried that, no love. Doing so produces a MessageBox at runtime saying Cannot perform 'Mod' operation on System.Double and System.Double. What I'm trying to do is get around the lack of bitwise operation support in ADO.Net expressions. In doing so I was hoping to be able to easily bind some control properties to boolean values in a datatable whose values are determined by an expression (somecolumn/somenumber)%2=1. Unfortunately I don't think this is possible. Oh, and the title, is an oops. What I meant was 3/4%2.

          L 1 Reply Last reply
          0
          • J jchalfant

            While C# can handle double % double, ADO.Net expressions can't. I've tried that, no love. Doing so produces a MessageBox at runtime saying Cannot perform 'Mod' operation on System.Double and System.Double. What I'm trying to do is get around the lack of bitwise operation support in ADO.Net expressions. In doing so I was hoping to be able to easily bind some control properties to boolean values in a datatable whose values are determined by an expression (somecolumn/somenumber)%2=1. Unfortunately I don't think this is possible. Oh, and the title, is an oops. What I meant was 3/4%2.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            jchalfant wrote:

            What I meant was 3/4%2.

            That makes sense. Maybe you can return the boolean values directly in your result set or however you retrieve them.

            xacc.ide - now with IronScheme support
            IronScheme - 1.0 alpha 2 out now

            J 1 Reply Last reply
            0
            • L leppie

              jchalfant wrote:

              What I meant was 3/4%2.

              That makes sense. Maybe you can return the boolean values directly in your result set or however you retrieve them.

              xacc.ide - now with IronScheme support
              IronScheme - 1.0 alpha 2 out now

              J Offline
              J Offline
              jchalfant
              wrote on last edited by
              #6

              That's one of my conclusions. But what I think I'll do instead is manually add the column to the datarow and evaluate the value based on other column values. I'm not pressed for time on this project, so I've got time to play around and do different things.

              1 Reply Last reply
              0
              • J jchalfant

                Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like: Convert((SomeColumn/4),'System.Int32')%2 = 1 Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Perhaps you should explain what it is that you are trying to accomplish, rather than asking about why the workaround that you think might be the solution doesn't work as expected...

                Despite everything, the person most likely to be fooling you next is yourself.

                J 1 Reply Last reply
                0
                • G Guffa

                  Perhaps you should explain what it is that you are trying to accomplish, rather than asking about why the workaround that you think might be the solution doesn't work as expected...

                  Despite everything, the person most likely to be fooling you next is yourself.

                  J Offline
                  J Offline
                  jchalfant
                  wrote on last edited by
                  #8

                  Your words of encouragement enlighten me... To summize what I've already said: Bitwise operation on the DataColumn Expression property to be used with boolean properties on databound controls. At any rate, I've determined that it ain't gonna happen through the dataset designer. Any sentiments of wanting to assist are much appreciated.

                  1 Reply Last reply
                  0
                  • J jchalfant

                    Annoyingly enough, DataColumn.Expression doesn't support bitwise operators, so I decided to do it the long way. Also, annoyingly I can't modulo a double by an int, thus forcing me to convert to int. This leaves me with a column expression that looks like: Convert((SomeColumn/4),'System.Int32')%2 = 1 Lo and behold my ire as I realize that the Convert function rounds the argument to its nearest whole value. Thus 3/4 = .75 Then: Convert(.75,Int32) = 1 and 1%2 = 1 This irritates me, especially since I'm trying to work through the DataSet Designer in VS 2k5 and subsequently trying to write as little code as possible. I've got a couple ideas for working around this - mostly involving writing in my own events or properties - but I'm curious to see if anyone has any other solutions.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, Not sure why SomeColumn would be a double to start with. assuming your SomeColumn actually holds an integer value, FIRST convert it to int, then start processing its bits. Probably a simple if (intValueOfSomeColumn & 4 != 0) ... does exactly what you want... :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                    J 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi, Not sure why SomeColumn would be a double to start with. assuming your SomeColumn actually holds an integer value, FIRST convert it to int, then start processing its bits. Probably a simple if (intValueOfSomeColumn & 4 != 0) ... does exactly what you want... :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                      J Offline
                      J Offline
                      jchalfant
                      wrote on last edited by
                      #10

                      I wish it were so simple. Datacolumn.Expression, any expression in an activex data object actually, doesn't support bitwise operators. So, that's no big deal x/y%2 should work. Right? Nope. ADO.net objects return a double to the engine before computing the modulus. So int/int%2 turns into double%2, which isn't supported. Convert won't work because it performs a Round instead of a cast. In the end, what I wound up settling on was computing the boolean values as bit fields when I retrieve the data from the database.

                      L 1 Reply Last reply
                      0
                      • J jchalfant

                        I wish it were so simple. Datacolumn.Expression, any expression in an activex data object actually, doesn't support bitwise operators. So, that's no big deal x/y%2 should work. Right? Nope. ADO.net objects return a double to the engine before computing the modulus. So int/int%2 turns into double%2, which isn't supported. Convert won't work because it performs a Round instead of a cast. In the end, what I wound up settling on was computing the boolean values as bit fields when I retrieve the data from the database.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        Hi, Seems to me one of us is not understanding what the other meant. Either you start with a TYPE double that holds an integer VALUE, then my advice is to convert it to a TYPE int; that will succeed without problems (unless you exceed the int's range). Or your TYPE double holds a non-integer VALUE, and then you will not be able to explain to me what the meaning of most of its bits is; your question does not make sense in this case. Did you at all try what I suggested? :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                        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