MrPickle5
MrPickle5

Reputation: 522

Syntax error with macro using bitwise operators

So I wrote a macro that swaps two numbers using the XOR bitwise operator. The algorithm using XOR gates to swap numbers is well-known (i.e. a ^= b; b ^= a; a ^= b;), but I keep getting syntax errors when calling the macro. It says it expects a semi-colon somewhere.

Exact error:

Line 73: error C2146: syntax error : missing ';' before identifier 'a'

Here is the file my program is having trouble with.

#define swapNumbers(x, y, z) z = x, x = y, y = z;
#define swapStrings(str1, str2, str3)  str3 = str1, str1 = str2, str2 = str3;
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;


#include <iostream>
#include "protocol.h"
#include <string>

void PrintMenu()
{
std::cout << "\n\nChapter 16 -- Learn By Doings " << std::endl;
std::cout << "\n1. Learn By Doing 16.4 " << std::endl;
std::cout << "2. Learn By Doing 16.5 " << std::endl;
std::cout << "3. Learn By Doing 16.6 " << std::endl;
std::cout << "4. Exit " << std::endl;
NewLine();
}

void GetMenuChoice(int &menuChoice)
{
std::cin >> menuChoice;
}

void ExecuteMenuChoice(int &menuChoice)
{
switch(menuChoice)
{
case 1:
    {
        //Learn By Doing 16.4

        //Swap two numbers
        int x = 10;
        int y = 20;
        int z = 30;

        std::cout << "\nBefore swapping x is " << x << " and y is " << y << std::endl;
        swapNumbers(x, y, z);
        std::cout << "After swapping x is " << x << " and y is " << y << std::endl; 
        NewLine();

        //Swap two cStrings
        char * firstName = "Magnus";
        char * lastName = "Carlsen";
        char * tempName = "GOAT";

        std::cout << "\nBefore swapping, first name is " << firstName << " and last name is " << lastName << std::endl;
        swapStrings(firstName, lastName, tempName);
        std::cout << "After swapping, first name is " << firstName << " and last name is " << lastName << std::endl;

    }
    break;
case 2:
    //Learn By Doing 16.5
    {

    int x = 0;

    //Check if number is power of two
    std::cout << "\nEnter number: " << std::endl;
    std::cin >> x;

    NewLine();
    std::cout << ChangeBoolToString(isPowerOfTwo(x));

    //Swap numbers using bitwise operations
    NewLine();
    int a = 666;
    int b = 777;

    std::cout << "\n\nBefore swapping a is " << a << " and b is " << b << std::endl;
    swapUsingXOR(a, b);
    std::cout << "After swapping a is " << a << " and b is " << b << std::endl;

    }
    break;
case 3:
    //Learn By Doing 16.6

    break;
case 4:
    //Exit
    NewLine();

    break;
default:
    std::cout << "Invalid input.  Please enter a number between 1 and 4. " << std::endl;

}
}

void NewLine()
{
std::cout << ' ' << std::endl;
}

bool isPowerOfTwo(int binaryNumber)
{
while(((binaryNumber & 1) == 0) && binaryNumber > 1)
    binaryNumber >>= 1;

if(binaryNumber == 1)
    return true;
else
    return false;
}

char * ChangeBoolToString(bool function)
{
if(function == true)
    return "Your number is a power of two! ";
else
    return "Your number is not a power of two. ";
}

Upvotes: 0

Views: 1377

Answers (3)

Nik Bougalis
Nik Bougalis

Reputation: 10613

This is flat out HORRIBLE. Do not do it. Referencing the same argument in a macro more than once is a recipe for disaster... Tell me what your code does with this:

int *f1(); // function that returns an int pointer.
int *f2(); // function that returns an int pointer.
int x = 666;

// Quick... how many times is each function called? And is that 
// what the programmer expects based on the regular semantics of
// of the language? Are you sure it's safe to call those functions
// multiple times? Will the result be what you think it's going to be?
swapUsingXOR(*f1(), *f2());

// And what happens here?
swapUsingXOR(x, x);

Upvotes: 1

Mitch
Mitch

Reputation: 44

Read about #define more...

#define is as you know for macros, however you put semicolons ";" in there! Remember, when you use #define is basically just adds a find and replace note for the pre-processor... Well replacing swapNumbers(x, y, z) with z = x, x = y, y = z; causes a problem... It keeps the semicolon ";", and makes an error...

For example, it can put semicolons in places they shouldn't be, and swapNumbers(x, y, z); Makes two at the end!

Read more here :)

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 225022

You have an extra space in your macro definition:

#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                    ^
                    |--- get rid of this space

The semicolon isn't necessary either. You might want to add some braces or use the do/while(0) trick to make your macros behave more like simple statements.

If you're a beginner (and it seems that you are), you probably want to check out clang. It produces a lot more helpful error messages in most cases than most other compilers do. For example, a test program with your macro gives these messages, which make it very clear what's going on:

example.c:14:5: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
    ^~~~~~~~~~~~
example.c:5:23: note: expanded from macro 'swapUsingXOR'
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                      ^
example.c:14:5: error: expected ';' after expression
    swapUsingXOR(a, b);
    ^
example.c:5:29: note: expanded from macro 'swapUsingXOR'
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                            ^
example.c:14:18: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
                 ^
example.c:14:5: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
    ^~~~~~~~~~~~
example.c:5:26: note: expanded from macro 'swapUsingXOR'
#define swapUsingXOR (a, b) a = a ^ b, b = b ^ a, a = a ^ b;
                         ^
example.c:14:21: warning: expression result unused [-Wunused-value]
    swapUsingXOR(a, b);
                    ^
4 warnings and 1 error generated.

Upvotes: 4

Related Questions