Work it out

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

    #1

    Work it out

    I was given a 'C' code to dry run and get the output.
    Plz help me out with the output.

    Int a=15,b=10;
    b<<=a;

    wht value would it yield?And plz if posible state the reason.

  • Jordan Abel

    #2
    Re: Work it out

    On 2006-01-26, cogno_byte <b.rocks.domain @gmail.com> wrote:[color=blue]
    > I was given a 'C' code to dry run and get the output.
    > Plz help me out with the output.
    >
    > Int a=15,b=10;[/color]

    You misspelled "int".
    [color=blue]
    > b<<=a;
    >
    > wht value would it yield?And plz if posible state the reason.[/color]

    On a 16-bit system, this is undefined behavior because it causes integer
    overflow. A plausible value on a 16-bit system might be 0. If int is at
    least 19 bits, the result will be 0x50000, or 327680.

    Comment

    • pemo

      #3
      Re: Work it out

      cogno_byte wrote:[color=blue]
      > I was given a 'C' code to dry run and get the output.
      > Plz help me out with the output.
      >
      > Int a=15,b=10;
      > b<<=a;
      >
      > wht value would it yield?And plz if posible state the reason.[/color]

      Why don't you try it?

      int a=15,b=10;

      printf("%d\n", b<<=a);

      b <<= a

      means

      b = b << a

      b's value is set to b's value left-shifted a bits.

      --
      ==============
      *Not a pedant*
      ==============


      Comment

      • Keith Thompson

        #4
        Re: Work it out

        "cogno_byte " <b.rocks.domain @gmail.com> writes:[color=blue]
        > I was given a 'C' code to dry run and get the output.[/color]
        [...]

        So why didn't you?

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Ico

          #5
          Re: Work it out

          Jordan Abel <random832@gmai l.com> wrote:[color=blue]
          > On 2006-01-26, cogno_byte <b.rocks.domain @gmail.com> wrote:[color=green]
          >> I was given a 'C' code to dry run and get the output.
          >> Plz help me out with the output.
          >>
          >> Int a=15,b=10;[/color]
          >
          > You misspelled "int".[/color]

          He also misspelled 'please'[color=blue]
          >[color=green]
          >> b<<=a;
          >>
          >> wht value would it yield?And plz if posible state the reason.[/color][/color]

          and here he does it again, now with the word 'what'

          Cogno_byte, if you want people to take a serious look at your questions,
          a good start would be to state them in proper english. Nobody expects
          you to use perfect language, but all those intentional abbreviations
          make your message only harder to read.


          --
          :wq
          ^X^Cy^K^X^C^C^C ^C

          Comment

          • CBFalconer

            #6
            Re: Work it out

            pemo wrote:[color=blue]
            > cogno_byte wrote:
            >[color=green]
            >> I was given a 'C' code to dry run and get the output.
            >> Plz help me out with the output.
            >>
            >> Int a=15,b=10;
            >> b<<=a;
            >>
            >> wht value would it yield?And plz if posible state the reason.[/color]
            >
            > Why don't you try it?
            >
            > int a=15,b=10;
            > printf("%d\n", b<<=a);
            >
            > b <<= a
            >
            > means
            >
            > b = b << a
            >
            > b's value is set to b's value left-shifted a bits.
            >
            > ==============
            > *Not a pedant*
            > ==============[/color]

            Your snippet will not compile, and even if you add the appropriate
            things the left shift operation on an integer is very likely to
            cause overflow and undefined behaviour. This is very useful when
            demonstrating your nifty software to the VP for marketing, or a
            prospective funder for the company, etc.

            Such sloppy coding deserves an ignominous burial. Up the pedants.

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson
            More details at: <http://cfaj.freeshell. org/google/>


            Comment

            Working...