[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...write a generic c function which will take any...

<< Back to general questions

Question by Shailender Govil
Submitted on 12/11/2003
Related FAQ: N/A
Rating: Rate this question: Vote
I want to write a generic c function which will take any basic data type and returns its binay reverse value.

For example

If pass Integer and value is 10 (0x00001010- binary equivalent).

the function should return (0x01010000).


Answer by Ledom Ipsong
Submitted on 1/31/2004
Rating:  Rate this answer: Vote
The fastest and easiest way that I have thought of to do this is by using a table, like this:<p>  int reverse_bits( int reverseme ) {<p>              int holder = reverseme; // the holder will be used to                                                      // examine the value while                                                      // reverseme gets changed<p>              switch( holder & 0x000f ){ // looking at the lowest four bits. . .             case 0x0000: reverseme = 0x0000; break;             case 0x0001: reverseme = 0x8000; break;             case 0x0002: reverseme = 0x4000; break;             case 0x0003: reverseme = 0xc000; break;             case 0x0004: reverseme = 0x2000; break;             // and so on. . .<br             case 0x000f: reverseme = 0x00f0; break;             }<p>              switch( holder & 0x00f0 ){ // the next four bits             case 0x0000: reverseme |= 0x0000; break;             case 0x0010: reverseme |= 0x0800; break;             case 0x0020: reverseme |= 0x0400; break;             case 0x0030: reverseme |= 0x0c00; break;             // have to use binary OR assignment operator             // in this and the other two switches to turn on             // the appropriate bits without messing with the             // others.               case 0x00f0: reverseme |= 0x00f0; break;              }<p>              // two more switches for the other two sets of              // four bits, then<p>              return reverseme; }<p>  Obviously, you have to do some work by hand this way, figuring out the opposites of a few bit patterns. But since you do it four bits at a time, there are only 16 patterns to deal with; it shouldn't take more than 5 minutes.<p> To do a function that takes any basic type, you'd have to cast your variable as a long when you call the function ( and change the function arg to be a long, o/c ). You can't do bitwise operations on floating point variables. Also, you're going to have to operate on a lot more bits than necessary for chars or ints. It usually makes more sense to make a separate function for each type, esp. when you have to do bitwise operations. Here, you can get away with three: call them lreverse_bits ( for doubles and longs ), ireverse_bits ( for floats and ints ) creverse_bits ( for chars ). They'll be very similar ( do the longs first, and you'll probably be able to cut and paste everything from that to the other two). The other way to avoid doing too much operation is to pass another argument that tells the function how many bits it actually needs to turn around, and stop after those.<p> As a final note, 0xffff notation is for hexadecimal, not binary. Each digit after the x represents four bits exactly, which makes it really easy for doing bitwise stuff; in decimal, things get a little weird. Octal is also good, but each digit represents 3 bits, so it doesn't work out quite as nicely. Octal is indicated by a leading zero, e.g. 031 = 25 in decimal. AFAIK, there's no standard way to indicate you're notating in binary (well, you can write: BIN 01010010). This probably has to do with the fact that there are no binary constants in C.<p> Good luck!

 

Answer by slarun
Submitted on 5/21/2004
Rating: Not yet rated Rate this answer: Vote
#include <stdio.h>
#include <math.h>

#define SBYTE 8
#define SSBYTE 16
#define IBYTE 32

void ByteShift(int);
void ShortByteShift(short);
void IntShift(int);

int main()
{
   int nNum;

   printf("Enter a number:");
   scanf("%d", &nNum);

   if (nNum <= 255) {
      printf("The nNum is 1 byte..\n");
      ByteShift(nNum);
   }
   else if (nNum <= 65535) {
      ShortByteShift(nNum);
      printf("The nNum is 2 byte...\n");
   }
   else{
      IntShift(nNum);
      printf("The nNum is 4 byte..\n");
   }

   return 0;   
}

void ByteShift(int nVal)
{
   int nLoop;
   int nShift;
   int nRes;
   int nCount;
   int nTmp;

   int nValue;

   nTmp = 0;
   nRes = 0;
   nValue = nVal;
   
   for(nLoop=0;nLoop<SBYTE;nLoop++)
   {
      nCount=nLoop+1;
      nTmp=(int)pow(2,nLoop);
      nTmp &= nValue;
      if (nTmp > 0) {
         nShift = 0;
         nShift = SBYTE - (nLoop + nCount);
         if(nShift > 0)
            nTmp = nTmp << nShift;
         else
            nTmp = nTmp >> abs(nShift);
         nRes |= nTmp;
      }
   }
   printf("Value is : %d\n", nRes);
}

void ShortByteShift(short sVal)
{
   int nLoop;
  int nShift;
  int nRes;
  int nCount;
  int nTmp;

  int nValue;

  nTmp = 0;
  nRes = 0;
  nValue = sVal;

  for(nLoop=0;nLoop<SSBYTE;nLoop++)
  {
      nCount=nLoop+1;
      nTmp=(int)pow(2,nLoop);
      nTmp &= nValue;
      if (nTmp > 0) {
        nShift = 0;
          nShift = SSBYTE - (nLoop + nCount);
        if(nShift > 0)
          nTmp = nTmp << nShift;
        else
          nTmp = nTmp >> abs(nShift);
        nRes |= nTmp;
      }
  }
  printf("Value is : %d\n", nRes);
   return;
}

void IntShift(int nVal)
{
   int nLoop;
  int nShift;
  int nRes;
  int nCount;
  int nTmp;

  int nValue;

  nTmp = 0;
  nRes = 0;
  nValue = nVal;

  for(nLoop=0;nLoop<IBYTE;nLoop++)
  {
    nCount=nLoop+1;
    nTmp=(int)pow(2,nLoop);
    nTmp &= nValue;
    if (nTmp > 0) {
      nShift = 0;
        nShift = IBYTE - (nLoop + nCount);
      if(nShift > 0)
        nTmp = nTmp << nShift;
      else
        nTmp = nTmp >> abs(nShift);
      nRes |= nTmp;
    }
  }
  printf("Value is : %d\n", nRes);
   return;
}
//gcc -lm <filename>

 

Answer by j
Submitted on 9/20/2006
Rating: Not yet rated Rate this answer: Vote
C Functions
(a) Need not return any value
(b) Should always return an integer
(c) Should always return a float
(d) Should always return more than one value

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to general questions


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.