By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

How to swap the nibbles whit dev++?

P: 2
Hello, i've got a project and im missing one of the parts "yes i've just started whit programming"
I'm using Dev++

How to swap the nibbles.

Expand|Select|Wrap|Line Numbers
  1. Ect   1010 - 1101
  2.    To 1101 - 1010
  3. "
  5. #define _swapNibble(x) ((x<<4)|(x>>4))
  7. int main()
  8. {
  9.     // On executing you will get
  10.     // x before swap = 0x1c
  11.     // x after swap = 0xc1
  12.     char x = 0x1c;
  13.     printf("x before swap = %x\n",x);
  15.     _swapNibble(x);
  16.     printf("x afer swap = %x",x);
  17. }
This is what i've got but got some faults"

Thank you.
Oct 16 '13 #1
Share this Question
Share on Google+
4 Replies

P: 2
Change the #define to:

#define _swapNibble(x) (((x<<4)|(x>>4))&0xFF)


#define _swapNibble(x) ((char)((x<<4)|(x>>4)))
Oct 16 '13 #2

P: 2
In function `int main()':

\c++\Calc\tip\SAH\you.cpp:10: error: `printf' undeclared (first use this function)

: error: (Each undeclared identifier is reported only once for each function it appears in.)

Execution terminated

. i can't see what i've done wrong..
Oct 16 '13 #3

P: 2
At the beginning of your code put:

#include <stdio.h>
Oct 16 '13 #4

Expert 100+
P: 2,398
Line 12: you declare x as a char. This type should only be used for characters. If you want a small integral type then use either signed char or unsigned char, depending on whether or not you want the value to be signed. (However, in subsequent comments I argue for using type int.)

Line 5: your macro is named _swapNibble. The C Standard reserves all names with a leading underscore just in case they are needed in future versions of the Standard. You should not use a reserved name. I suggest you remove the leading underscore.

Line 13: conversion specifier %x expects an int argument; however you pass x, which is a char. This is a good reason to declare x as an int.

Line 5: the expression that the macro expands to has type int despite the operand being type char. This is another good reason to declare x as an int.

Line 15: this statement evaluates an expression and then discards the result. You need an assignment statement if you want to assign the result to some variable.
Oct 17 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.