Thread: Bitwise Operators

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question Bitwise Operators

    Hi,

    I have a function (prototype):

    Code:
    void MouseButtonDown(int x, int y, int iBut:2);


    Code:
    void MouseButtonDown(int x, int y, int iBut)
    {
    	switch (iBut)
    	{
    	case (00):
    		{
    etc...
    Is this valid?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are you trying to achieve? I believe your function prototype is syntactically invalid.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm with laserlight: Unless you have compiler extensions that allow you to pass values like that, the code should not compile.

    If you are using C++, you could use templates to implement numbers that are specific number of bits, but there's no space benefit in doing so.

    Bitfields are only allowed in structs/classes (and perhaps unions).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    He probably wants to have a 2bit variable. I don't think that is possible.
    As far as I know you can have bit fields only in a struct. Like:
    Code:
    typdef struct {
       int twoBit:2;
    } smallInt;
    Now, the point is that you will declare the function normally to accept an int. Then you will make a struct that has one variable with :2. So it will be a struct which has a twoBit variable. Then you will pass the smallInt.twoBit int to the function and you will have what you want. You won't do though case (00): You will do normally case (0), case(1), case(2), case(3).
    If you assign a value greater than 3 on a 2 bit int then only the 2 last digits will be assign, thus, since 4 = 100 you will have 00, thus your int will be 0.

    Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Smile

    Quote Originally Posted by C_ntua View Post
    He probably wants to have a 2bit variable. I don't think that is possible.
    As far as I know you can have bit fields only in a struct. Like:
    Code:
    typdef struct {
       int twoBit:2;
    } smallInt;
    Now, the point is that you will declare the function normally to accept an int. Then you will make a struct that has one variable with :2. So it will be a struct which has a twoBit variable. Then you will pass the smallInt.twoBit int to the function and you will have what you want. You won't do though case (00): You will do normally case (0), case(1), case(2), case(3).
    If you assign a value greater than 3 on a 2 bit int then only the 2 last digits will be assign, thus, since 4 = 100 you will have 00, thus your int will be 0.

    Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.
    Excellent. Thanks m8!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.
    Indeed, so if the reason is to save space, then perhaps it is misguided. If the reason is to restrict the range to 4 values, then perhaps the real reason for the range is that there are a specific number of say, events, that are acceptable, so perhaps an enum or a full blown class tht emulates an enum would be better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    And you can combine the enums with the | operator. So you can pass two or three options with one parameter. Like MouseMoveDown(...., option1 | option2 | option 3) and then use a mask (using the &) to get individual bits/options

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM