Thread: Nibbles program...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    54

    Nibbles program...

    Hai can any one explain this program to me in the clear way . I am not able to understand this code clearley so i am asking to you . Thanks in advance
    Code:
    #include <stdio.h> 
    unsigned char swap_nibbles(unsigned char c) 
    { unsigned char temp1 , temp2; 
    temp1  = c & 0 x0F; 
    temp2  = c & 0 xF0; 
    temp1 =temp1  << 4; 
    temp2 =temp2  >> 4; 
    return(temp2 |temp1); //adding the bits 
    } int main(void) 
    { char ch=0 x34; 
    printf("\nThe exchanged value is %x",swap_nibbles(ch)); 
    return 0; 
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Jeesh, how about some formating?
    Code:
    #include <stdio.h> 
    
    unsigned char swap_nibbles(unsigned char c) 
    {
        unsigned char temp1 , temp2; 
        temp1  = c & 0x0F;   // Zeroes last 4 bits of c (preserves first 4)
        temp2  = c & 0xF0;   // Zeroes first 4 bits of c (preserves last 4)
        temp1 =temp1  << 4;   // Shifts first 4 bits so they are the last 4 bits
        temp2 =temp2  >> 4;   // Shifts last 4 bits so they are the first 4 bits
        return(temp2 |temp1); // Combines the results of the first and last 4 bits so
                              // they wind up swapped with each other based on original c value
    }
    
    int main(void) 
    {
        unsigned char ch=0x34;
        printf("\nThe exchanged value is &#37;x",swap_nibbles(ch)); 
        return 0; 
    }
    Last edited by hk_mp5kpdw; 10-22-2007 at 05:38 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Thank you for your replay...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM