Bitwise Operators

This is a discussion on Bitwise Operators within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a function (prototype): Code: void MouseButtonDown(int x, int y, int iBut:2); Code: void MouseButtonDown(int x, int y, ...

  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
    21,936
    What are you trying to achieve? I believe your function prototype is syntactically invalid.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,936
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM

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