Re: Regarding left shift operator

Discussion in 'C++' started by Larry Brasfield, Jan 5, 2005.

  1. > Respected Experts,
    >
    > Iam RaviKumar.N, I have a query regarding left shift operator.
    >
    > Consider the following variables:
    > 1) unsigned char a = 2;
    > 2) unsigned char b = 3;
    > 3) unsigned short c;
    >
    > Consider the following expression:
    >
    > c = (unsigned short)(( a << 8) | b);


    Better groups for this are: comp.lang.c or comp.lang.c++ .
    Followups set accordingly.

    > My query is:
    > "Is it valid to shift a byte value by an 8-bit".


    An interesting question, but your code does not do that.
    In the expression '(a << 8)', the value taken from the object
    named 'a' is promoted to int before the shift.

    > I got this expression compiled,but Iam not sure about the results I got.
    > Please let me know about this.


    What about your results made you unsure?

    > With Regards
    > Ravi Kumar.N


    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
    "Ravi kumar.N" <> wrote in message news:...
     
    Larry Brasfield, Jan 5, 2005
    #1
    1. Advertising

  2. Larry Brasfield wrote:

    [...]
    >> I got this expression compiled,but Iam not sure about the results I got.
    >> Please let me know about this.

    >
    >
    > What about your results made you unsure?


    I think that he expected the result to be 3, because he shifted out the
    width of an char, but the compiler, as you said, do this shift operation
    with ints, and so the results are others than expected by the OP.

    To the OP:

    If you had used unsigned char as result type and not short, than the
    result would be 3, but as you used short, the real value was printed,
    3 or ^C, because unsigned char and signed chars are interpreted as
    Letters of the underlying character set by the output streams, they are
    only internally numbers ;-).

    Kind Regards,
    Nicolas
     
    Nicolas Pavlidis, Jan 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wenjie
    Replies:
    3
    Views:
    1,041
    Ron Samuel Klatchko
    Jul 11, 2003
  2. Larry Brasfield

    Re: Regarding left shift operator

    Larry Brasfield, Jan 5, 2005, in forum: C Programming
    Replies:
    1
    Views:
    362
    Nicolas Pavlidis
    Jan 5, 2005
  3. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,462
    CBFalconer
    Nov 30, 2006
  4. Sanny
    Replies:
    38
    Views:
    3,433
    Thomas Richter
    Apr 29, 2011
  5. pc
    Replies:
    2
    Views:
    1,325
    crisgoogle
    Jun 8, 2011
Loading...

Share This Page