CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS LANGUAGES C DETAILS
    Question :
    How do I swap bytes?
    Category C Interview Questions
    Rating (0.3) By 9 users
    Added on 11/17/2011
    Views 166
    Rate it!
    Answers:

    V7 Unix had a swab function, but it seems to have been forgotten.
    A problem with explicit byte-swapping code is that you have to decide whether to call it or not, based on the byte order of the data and the byte order of the machine in use.
    A better solution is to define functions which convert between the known byte order of the data and the (unknown) byte order of the machine in use, and to arrange for these functions to be no-ops on those machines which already match the desired byte order. A set of such functions, introduced with the BSD networking code but now in wide use, is ntohs, htons, ntohl, and htonl. These are intended to convert between ``network'' and ``host'' byte orders, for ``short'' or ``long'' integers, where ``network'' order is always big-endian, and where ``short'' integers are always 16 bits and ``long'' integers are 32 bits. (This is not the C definition, of course, but it's compatible with the C definition) So if you know that the data you want to convert from or to is big-endian, you can use these functions. (The point is that you always call the functions, making your code much cleaner. Each function either swaps bytes if it has to, or does nothing. The decision to swap or not to swap gets made once, when the functions are implemented for a particular machine, rather than being made many times in many different calling programs.)

    If you do have to write your own byte-swapping code, the two obvious approaches are again to use pointers or unions. Here is an example using pointers:
    void byteswap(char *ptr, int nwords) {
    char *p = ptr;
    while(nwords-- > 0) {
    char tmp = *p;
    *p = *(p + 1);
    *(p + 1) = tmp;
    p += 2;
    }
    }

    And here is one using unions:

    union word
    {
    short int word;
    char halves[2];
    };

    void byteswap(char *ptr, int nwords)
    {
    register union word *wp = (union word *)ptr;
    while(nwords-- > 0) {
    char tmp = wp->halves[0];
    wp->halves[0] = wp->halves[1];
    wp->halves[1] = tmp;
    wp++;
    }
    }

    These functions swap two-byte quantities; the extension to four or more bytes should be obvious. The union-using code is imperfect in that it assumes that the passed-in pointer is word-aligned. It would also be possible to write functions accepting separate source and destination pointers, or accepting single words and returning the swapped values.



    If you have the better answer, then send it to us. We will display your answer after the approval.
    Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    How can I convert integers to binary or hexadecimal?
    View Answer
    Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?
    View Answer
    What is the most efficient way to count the number of bits which are set in an integer?
    View Answer
    What is the best way of making my program efficient?
    View Answer
    Are pointers really faster than arrays?
    View Answer
    I have been replacing multiplications and divisions with shift operators, because shifting is more efficient.
    View Answer
    People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed
    View Answer
    How can I swap two values without using a temporary?
    View Answer
    Which is more efficient, a switch statement or an if else chain?
    View Answer
    Is there a way to switch on strings?
    View Answer
    Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?
    View Answer
    Are the outer parentheses in return statements really optional?
    View Answer
    Why dont C comments nest?
    View Answer
    Why isnt there a numbered, multi-level break statement to break out
    View Answer
    There seem to be a few missing operators ...
    View Answer
    Does C have circular shift operators?
    View Answer
    Is C a great language, or what?
    View Answer
    If the assignment operator were ...
    View Answer
    Does C have an equivalent to Pascals with statement?
    View Answer
    Why doesnt C have nested functions?
    View Answer


    Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

    View ALL C Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2014 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0.015625 Seconds