Thread: Bitshift syntax error

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196

    Bitshift syntax error

    Hey,

    This is giving me an error, and I'm not sure why:
    Code:
    (stu->attr >> STUDENT_ID_ACCESS_SHIFT) |= (student_number & STUDENT_ID_MASK);
    Student is a struct, attr is an int used for bit storage. My hopes was to shift those bits (since I'm storing them in the high order bits) all the way down to the low order, then storing the number in there. The reason I'm masking is because I only want 14 bits of it (5 digit student number max).

    Can anybody throw me a bone here on what is wrong?

    Thanks.

    - Greg

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    1
    left is an express, you can't assign value to an expression, just break it apart:
    a = (stu->attr >> STUDENT_ID_ACCESS_SHIFT);
    a |= (student_number & STUDENT_ID_MASK);

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by arisun View Post
    left is an express, you can't assign value to an expression, just break it apart:
    a = (stu->attr >> STUDENT_ID_ACCESS_SHIFT);
    a |= (student_number & STUDENT_ID_MASK);
    It will copy the ID stored in the attribute into a, then set it in the copy, that doesn't do what I need it to. I need to set it to the original.

    There has to be away to shift it to a certain location for bit setting..

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    I came up with this line, it should work how I want it to, no?

    Code:
    stu->attr |= (STUDENT_ID_ACCESS_SHIFT << (sstudent_number & STUDENT_ID_MASK));
    Hope that does what I want - think it will..

    Thanks for the help, I don't get expressions vs statements or what have you very well, but I generally don't do operations like that, either.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    This is difficult to follow, and the reason I use #defines for bitvector work. Get the defines right, and it's easier to work with:
    Code:
    #define ST_NUM(x) ((x) & STUDENT_ID_MASK)
    #define ST_NUM_ATTR(x) (ST_NUM(x) << STUDENT_ID_ACCESS_SHIFT)
    #define ST_ATTR(x) ((x)->attr)
    then, you can make you line more readable:
    Code:
    ST_ATTR(stu) |= ST_NUM_ATTR(student_number);

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by Jamdog View Post
    This is difficult to follow, and the reason I use #defines for bitvector work. Get the defines right, and it's easier to work with:
    Code:
    #define ST_NUM(x) ((x) & STUDENT_ID_MASK)
    #define ST_NUM_ATTR(x) (ST_NUM(x) << STUDENT_ID_ACCESS_SHIFT)
    #define ST_ATTR(x) ((x)->attr)
    then, you can make you line more readable:
    Code:
    ST_ATTR(stu) |= ST_NUM_ATTR(student_number);
    Good call, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM