bit shifting uint8_t

This is a discussion on bit shifting uint8_t within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem and I can't figure it out! I have this variable that has the uint8_t type (and ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    bit shifting uint8_t

    I'm having a problem and I can't figure it out!

    I have this variable that has the uint8_t type (and yes I have to use this one for this project).

    First I had the problem of outputting it:

    uint8_t var = 4;
    cout << var << endl;

    would output a box. however the workaround is using:

    cout << (int)var << endl;

    But what I want to do is to shift the bits and the values do not make sense at all! Any ideas?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    cout and cin overload the bit shifting operators. If you want to shift bits than do so on a different line.

    uint8_t x = 0;
    x = x << 2;

    That shifts x two bits to the left.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I doubt that bit-shifting is the answer.

    Here's my guess at what's going on:

    Is uint8_t a type char?

    cout is smart. If you display a type char, cout will automatically display the ASCII character instead of the actual numerical value. Windows is dumb. It displays a box when it gets an invalid ASCII value.

    To confirm if this is the case, try changing var to 65, and you should see an 'A' instead of a box. (Or, try 52, and maybe you'll see a '4' !)

    I'm not sure how to force cout to display the value... maybe you need to use <iomanip>. OR, maybe this will work:
    cout << dec << x << endl;

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Or cast to an int.

  5. #5
    Me
    Join Date
    Jul 2006
    Posts
    71
    Yeah casting to an int would probably be the easiest solution.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Is uint8_t a type char?
    Most likely it's an unsigned char.

    It looks like the OP has already figured this out, however:
    First I had the problem of outputting it:
    Code:
    uint8_t var = 4;
    cout << var << endl;
    would output a box. however the workaround is using:
    Code:
    cout << (int)var << endl;
    As for this:
    But what I want to do is to shift the bits and the values do not make sense at all! Any ideas?
    If you're still casting the value to an int before you print it, and you do the shifting on a separate line to avoid cout's overloaded <<, then the problem must be with the shifting statement itself. Post the statement.

    Are you casting to int when you shift it?
    Code:
    uint8_t x = 40;
    x = (int)x >> 2;
    cout << (int)x << endl;
    Right-shifting signed numbers, such as ints, doesn't work. (I think; it may be left-shifting that doesn't work. Anyway, just don't cast to a signed type like int and you should be okay.)

    [edit] Found it: right-shifting negative signed numbers causes ones to appear rather than zeros (from http://cplus.about.com/od/advancedtu.../aa042203h.htm):
    What does this have to do with right shifts? [...] With a signed number, the sign bit is replicated and shifted from the left to replace bits. For positive signed numbers, the sign bit is zero, so zeros are shifted in. For negative signed numbers, the sign bit is one, so ones are shifted in.
    [/edit]

    All things considered, something like this should work:
    Code:
    uint8_t x = 100;
    x >>= 3;
    x <<= 2;
    cout << (int)x << endl;
    Last edited by dwks; 07-27-2006 at 12:21 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dwks
    [edit] Found it: right-shifting negative signed numbers causes ones to appear rather than zeros (from http://cplus.about.com/od/advancedtu.../aa042203h.htm):
    [/edit]
    Correction:
    The value of E1 >> E2 is E1 rightshifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 divided by the quantity 2 raised to the power E2. If E1 has a signed type and a negative value, the resulting value is implementation defined.
    One of the options, of course, is just as you say.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I didn't believe him, and set out to write an example to prove him wrong... ^_^. (I learned something, I guess.) Something from my assembly knowledge was trying to say I would be wrong...
    Personally, I think bit shifting on something with a sign is bound to get you into trouble... it doesn't make much sense.
    I have this variable that has the uint8_t type (and yes I have to use this one for this project).
    What's wrong with the (u)intX_t types? There are times when you need a variable that holds a set amount - I think those types fit the bill nicely.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    So, to sum it up for the OP:

    Instead of casting it to int, try the following:
    Code:
    uint8_t var = 4;
    cout << (unsigned int)var;
    If you cast it to simple int - as you tried yourself - the output value won't make sense indeed. Atleast it won't if you were expecting to see the number 4 on your screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 06:59 AM
  3. Quick question regardin bit shifting
    By shoobsie in forum C Programming
    Replies: 10
    Last Post: 11-04-2005, 09:46 AM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 07:20 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21