0

I've already created the macros for the assignment, but cannot find the syntax error I get when running the program. Any ideas would be greatly appreciated, thanks.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define ODD(X) ((X) & 01)
#define BITON(X,N) (((X) >> N) & 01)
#define ALLON(X,S,E) (((X) & ((((int) pow(2,(E-S))-1) << (E-S))) ^ (((int) pow(2,E-S)-1) << (E-S)))
//-----------------------------------------------------------------------------                                          
int main(void) {

  unsigned int U1,BitNumber,Start,End;

  printf("Enter an integer : ");
  scanf("%ud",&U1);
  printf("%u is %s\n",U1,ODD(U1)?"odd":"even");

  printf("Enter an integer and a bit number : ");
  scanf("%u %d",&U1,&BitNumber);
  printf("%u has bit %d %s\n",U1,BitNumber,BITON(U1,BitNumber)?"on":"off");

  printf("Enter an integer, start and end bit numbers : ");
  scanf("%u %u %u",&U1,&Start,&End);
  printf("%u has %s those bits on\n",U1,ALLON(U1,Start,End)?"all":"not all");

  return(EXIT_SUCCESS);
}

//----------------------------------------------------------------------------- 

Error:

BitOps.c: In function ‘main’:
BitOps.c:23:77: error: expected ‘)’ before ‘;’ token
   printf("%u has %s those bits on\n",U1,ALLON(U1,Start,End)?"all":"not all");
                                                                             ^
BitOps.c:26:1: error: expected ‘;’ before ‘}’ token
 }
6
  • 1
    The compiler does not tell you the line number of the syntax error? Strange. Commented Mar 27, 2018 at 17:31
  • #define BITON(X,N) (((X) >> N) & 01) Wouldn't ((X) & (1 << (N))) be better? Commented Mar 27, 2018 at 17:32
  • @bipll Depends on whether 0/1 is desired as result. Commented Mar 27, 2018 at 17:34
  • I copied the actual body of the program from the assignment itself, the only additions to the code should have been under the #define lines; that's why it's strange to see syntax errors. Commented Mar 27, 2018 at 17:35
  • Yes, sure. Converting a true value to 1 is the second operation anyway. Commented Mar 27, 2018 at 17:37

2 Answers 2

1

The macro definition of ALLON has an extra open parenthesis in it. The compiler can't be sure that it's wrong until it reaches the semicolon at the end of the line where ALLON is used, so the error message complains about that line (which is fine) rather than the definition of ALLON, but the definition of ALLON is where the problem is.

Sign up to request clarification or add additional context in comments.

Comments

0

Enable -Wall flags and compile the code, its very informative, don't ignore it. There are few bugs in your code as

  • Better 01 replace with 0x1 that serve actual purpose.
  • Read the warning, BitNumber is declared as unsigned but %d format specifier is used. Compile your code with -Wall flag.
  • ALLON() macro expansion is not correct, which I correctly added below.

Here is expected one

#define ODD(X) ((X) & 0x1)
#define BITON(X,N) (((X) >> N) & 0x1)
#define ALLON(X,S,E) (((X) & ( ((((int) pow(2,(E-S))-1) << (E-S))) ^ (((int) pow(2,E-S)-1) << (E-S)))) )

int main(void) {

  unsigned int U1,BitNumber,Start,End;

  printf("Enter an integer : ");
  scanf("%u",&U1);
  printf("%u is %s\n",U1,ODD(U1)?"odd":"even");

  printf("Enter an integer and a bit number : ");
  scanf("%u %u",&U1,&BitNumber);
  printf("%u has bit %d %s\n",U1,BitNumber,BITON(U1,BitNumber)?"on":"off");

  printf("Enter an integer, start and end bit numbers : ");
  scanf("%u %u %u",&U1,&Start,&End);
  printf("%u has %s those bits on\n",U1,ALLON(U1,Start,End) ?"all":"not all");

  return(EXIT_SUCCESS);
}

5 Comments

Thank you, achal for your response. I'm now getting another error after changing my code per your suggestions: /tmp/ccrRqgmy.o: In function main': BitOps.c:(.text+0x12d): undefined reference to pow' BitOps.c:(.text+0x18d): undefined reference to `pow' collect2: error: ld returned 1 exit status
link with -lm. for e.g gcc -Wall test.c -lm
why are you even using pow ? that's strange
I think your assignment is also trying to figure out if you know how to remove the use of pow and understand what bit-shifting actually means.
Agree @AhmedMasud pow() is un-necessary here. OP should learn more of bit shifting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.