Results 1 to 3 of 3

Thread: Have I got them right?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21

    Have I got them right?

    I'd be obliged if someone could validate the functions I've written below from the try-it-yourself excercises of K&R:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void DWordToWords(const int DWord, short int* LoWord, short int* HiWord);
    void MakeDWord(const short LoWord, const short HiWord, int* DWord);
    unsigned GetBits(unsigned, int, int);
    unsigned SetBits(unsigned, int, int, int);
    unsigned Invert(unsigned, int, int);
    
    
    /*int main(void)
    {
    	int DWord;
    	short LoWord, HiWord;
    	
    	DWord = 1066825727;
    	LoWord = HiWord = 0;
    
    	DWordToWords(DWord, &LoWord, &HiWord);
    	printf("DWord = %d\nLoWord = %d\nHiWord = %d\n\n", DWord, LoWord, HiWord);
    
    	DWord = 0;
    	MakeDWord(LoWord, HiWord, &DWord);
    	printf("DWord = %d\nLoWord = %d\nHiWord = %d\n\n", DWord, LoWord, HiWord);
    }*/
    
    
    void DWordToWords(const int DWord, short int* LoWord, short int* HiWord)
    {
    	
    	*LoWord = DWord & ~(~0<<16);
    	*HiWord = DWord>>16;
    }
    
    void MakeDWord(const short LoWord, const short HiWord, int* DWord)
    {
    	*DWord = HiWord<<16;
    	*DWord |= LoWord;
    }
    
    unsigned int GetBits(unsigned x, int p, int n)
    {
    	/*gets the right-adjusted n bits of integer x 
    	starting from position p*/
    	return (x>>(p-n+1)) & ~(~0<<(p-n));
    }
    
    unsigned int SetBits(unsigned x, int p, int n, int y)
    {
    	/*set n bits of integer x starting from position 
    	p to the right-most n bits in integer y*/
    	y&=~(~0<<n);
    	return x|((y&~(~0<<n))<<(p-n+1));
    }
    
    unsigned Invert(unsigned x, int p, int n)
    {
    	/*Inverts n bits in x starting from position p*/
    	int temp=GetBits(x, p, n);
    	temp=~temp;
    	return SetBits(x, p, n, temp);
    }

  2. #2

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    And this one too:

    Code:
    unsigned RightRot(unsigned x, int n)
    {
    	/*Rotates x rightwards by n bit positions*/
    	int temp=GetBits(x, n-1,n);
    	x>>=n;
    	return SetBits(x,31,n,temp);
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    They look ok, although I find your bit pattern generation overly complicated.
    ~(~0<<16);
    We all know that
    0xFFFF0000
    is the same.

    Your placement of const in parameters is inconsistent too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width