wried integer addition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tikviva

    wried integer addition

    Hi, I have a question regarding to integer addition.

    Here is the problem.

    I have an integer, x = 0x00000001
    and I also have another integer, y = 0xffffff94

    Is there any method to concatenate x & y, so that x = 0x00000194 ?
    Thanks for the help. =)

    God bless,
    tikviva
  • Andrey Tarasevich

    #2
    Re: wried integer addition

    tikviva wrote:[color=blue]
    > ...
    > I have an integer, x = 0x00000001
    > and I also have another integer, y = 0xffffff94
    >
    > Is there any method to concatenate x & y, so that x = 0x00000194 ?
    > ...[/color]

    I don't see how concatenating 0x00000001 and 0xffffff94 might produce
    0x00000194. What kind of "concatenat ion" are you talking about? Why did
    you call it "concatenat ion" in the first place?

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Patrik Stellmann

      #3
      Re: wried integer addition

      tikviva wrote:
      [color=blue]
      > Hi, I have a question regarding to integer addition.
      >
      > Here is the problem.
      >
      > I have an integer, x = 0x00000001
      > and I also have another integer, y = 0xffffff94
      >
      > Is there any method to concatenate x & y, so that x = 0x00000194 ?
      > Thanks for the help. =)[/color]
      So you're looking for a function that takes 2 integers and returns
      0x00000194 if the input is 0x00000001 and 0xffffff94?

      int f(int x, int y)
      {
      return 0x00000194;
      }

      Ok, probably not what you were looking for but it's difficult to create
      a reasonable function when given only 1 input-output pair...

      Maybe you want to take the 1 of x and place at infront of the 94 of y?
      Then the function would look like this (i think):
      return (x << 8) | (y & 0xFF);

      Comment

      • Karl Heinz Buchegger

        #4
        Re: wried integer addition

        Patrik Stellmann wrote:[color=blue]
        >
        > tikviva wrote:
        >[color=green]
        > > Hi, I have a question regarding to integer addition.
        > >
        > > Here is the problem.
        > >
        > > I have an integer, x = 0x00000001
        > > and I also have another integer, y = 0xffffff94
        > >
        > > Is there any method to concatenate x & y, so that x = 0x00000194 ?
        > > Thanks for the help. =)[/color]
        > So you're looking for a function that takes 2 integers and returns
        > 0x00000194 if the input is 0x00000001 and 0xffffff94?
        >
        > int f(int x, int y)
        > {
        > return 0x00000194;
        > }
        >
        > Ok, probably not what you were looking for but it's difficult to create
        > a reasonable function when given only 1 input-output pair...
        >
        > Maybe you want to take the 1 of x and place at infront of the 94 of y?
        > Then the function would look like this (i think):
        > return (x << 8) | (y & 0xFF);[/color]

        You forgot about the 0xfffff part, which has to be masked away (if
        your theory of what the function should do holds)

        return ( (x & ~0xff) << 8 ) | ( y & 0xff );

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Karl Heinz Buchegger

          #5
          Re: wried integer addition

          Karl Heinz Buchegger wrote:[color=blue]
          >
          > Patrik Stellmann wrote:[color=green]
          > >
          > > tikviva wrote:
          > >[color=darkred]
          > > > Hi, I have a question regarding to integer addition.
          > > >
          > > > Here is the problem.
          > > >
          > > > I have an integer, x = 0x00000001
          > > > and I also have another integer, y = 0xffffff94
          > > >
          > > > Is there any method to concatenate x & y, so that x = 0x00000194 ?
          > > > Thanks for the help. =)[/color]
          > > So you're looking for a function that takes 2 integers and returns
          > > 0x00000194 if the input is 0x00000001 and 0xffffff94?
          > >
          > > int f(int x, int y)
          > > {
          > > return 0x00000194;
          > > }
          > >
          > > Ok, probably not what you were looking for but it's difficult to create
          > > a reasonable function when given only 1 input-output pair...
          > >
          > > Maybe you want to take the 1 of x and place at infront of the 94 of y?
          > > Then the function would look like this (i think):
          > > return (x << 8) | (y & 0xFF);[/color]
          >
          > You forgot about the 0xfffff part, which has to be masked away (if
          > your theory of what the function should do holds)
          >
          > return ( (x & ~0xff) << 8 ) | ( y & 0xff );
          >[/color]


          grr.
          That's the problem with bit operations: It is easy to read something
          into a source code which is not written there.

          Your function was fine.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • Patrik Stellmann

            #6
            Re: wried integer addition



            Karl Heinz Buchegger wrote:[color=blue]
            > Patrik Stellmann wrote:
            >[color=green]
            >>tikviva wrote:
            >>
            >>[color=darkred]
            >>>Hi, I have a question regarding to integer addition.
            >>>
            >>>Here is the problem.
            >>>
            >>>I have an integer, x = 0x00000001
            >>>and I also have another integer, y = 0xffffff94
            >>>
            >>>Is there any method to concatenate x & y, so that x = 0x00000194 ?
            >>>Thanks for the help. =)[/color]
            >>
            >>So you're looking for a function that takes 2 integers and returns
            >>0x00000194 if the input is 0x00000001 and 0xffffff94?
            >>
            >>int f(int x, int y)
            >>{
            >> return 0x00000194;
            >>}
            >>
            >>Ok, probably not what you were looking for but it's difficult to create
            >>a reasonable function when given only 1 input-output pair...
            >>
            >>Maybe you want to take the 1 of x and place at infront of the 94 of y?
            >>Then the function would look like this (i think):
            >> return (x << 8) | (y & 0xFF);[/color]
            >
            >
            > You forgot about the 0xfffff part, which has to be masked away (if
            > your theory of what the function should do holds)
            >
            > return ( (x & ~0xff) << 8 ) | ( y & 0xff );
            >[/color]

            Don't think so! The 0xffffff was only in y, not in x. Your function
            would return 0x00000094 because (x & ~0xff) equals 0

            Comment

            • David Harmon

              #7
              Re: wried integer addition

              On Fri, 20 Feb 2004 10:08:53 +0100 in comp.lang.c++, Karl Heinz
              Buchegger <kbuchegg@gasca d.at> was alleged to have written:[color=blue][color=green]
              >> You forgot about the 0xfffff part, which has to be masked away (if
              >> your theory of what the function should do holds)
              >>
              >> return ( (x & ~0xff) << 8 ) | ( y & 0xff );
              >>[/color]
              >
              >
              >grr.
              >That's the problem with bit operations: It is easy to read something
              >into a source code which is not written there.[/color]

              Perhaps you would prefer

              const int rad = 256;
              return (x * rad) + (y % rad);

              Comment

              • Geoffrey Mortimer

                #8
                Re: wried integer addition


                "tikviva" <tikviva@yahoo. com> wrote in message
                news:4b2febc7.0 402192257.baa91 10@posting.goog le.com...[color=blue]
                > Hi, I have a question regarding to integer addition.
                >
                > Here is the problem.
                >
                > I have an integer, x = 0x00000001
                > and I also have another integer, y = 0xffffff94
                >
                > Is there any method to concatenate x & y, so that x = 0x00000194 ?
                > Thanks for the help. =)
                >
                > God bless,
                > tikviva[/color]

                Homework?

                (x + 1) << 8 + y


                Comment

                Working...