473,382 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Have I done 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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void DWordToWords(const int DWord, short int* LoWord, short int*
  6. HiWord);
  7. void MakeDWord(const short LoWord, const short HiWord, int* DWord);
  8. unsigned GetBits(unsigned, int, int);
  9. unsigned SetBits(unsigned, int, int, int);
  10. unsigned Invert(unsigned, int, int);
  11.  
  12.  
  13. /*int main(void)
  14. {
  15. int DWord;
  16. short LoWord, HiWord;
  17.  
  18. DWord = 1066825727;
  19. LoWord = HiWord = 0;
  20.  
  21. DWordToWords(DWord, &LoWord, &HiWord);
  22. printf("DWord = %d\nLoWord = %d\nHiWord = %d\n\n", DWord, LoWord,
  23. HiWord);
  24.  
  25. DWord = 0;
  26. MakeDWord(LoWord, HiWord, &DWord);
  27. printf("DWord = %d\nLoWord = %d\nHiWord = %d\n\n", DWord, LoWord,
  28. HiWord);
  29. }*/
  30.  
  31.  
  32. void DWordToWords(const int DWord, short int* LoWord, short int*
  33. HiWord)
  34. {
  35.  
  36. *LoWord = DWord & ~(~0<<16);
  37. *HiWord = DWord>>16;
  38. }
  39.  
  40. void MakeDWord(const short LoWord, const short HiWord, int* DWord)
  41. {
  42. *DWord = HiWord<<16;
  43. *DWord |= LoWord;
  44. }
  45.  
  46. unsigned int GetBits(unsigned x, int p, int n)
  47. {
  48. /*gets the right-adjusted n bits of integer x
  49. starting from position p*/
  50. return (x>>(p-n+1)) & ~(~0<<(p-n));
  51. }
  52.  
  53. unsigned int SetBits(unsigned x, int p, int n, int y)
  54. {
  55. /*set n bits of integer x starting from position
  56. p to the right-most n bits in integer y*/
  57. y&=~(~0<<n);
  58. return x|((y&~(~0<<n))<<(p-n+1));
  59. }
  60.  
  61. unsigned Invert(unsigned x, int p, int n)
  62. {
  63. /*Inverts n bits in x starting from position p*/
  64. int temp=GetBits(x, p, n);
  65. temp=~temp;
  66. return SetBits(x, p, n, temp);
  67. }
  68.  
  69.  
Nov 14 '05 #1
2 2432

"Curious Student" <es*********@rediffmail.com> wrote

I'd be obliged if someone could validate the functions I've written
below from the try-it-yourself excercises of K&R:
The exercise is a bit out of date. On many platforms an int is 32 bits
whilst shorts are 16 bits, but this is by no means universal, nor are you
guaranteed that two shorts fit in an int.
Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h>
  2.  #include <stdlib.h>
  3.  #include <string.h>
  4.  void DWordToWords(const int DWord, short int* LoWord, short int*
  5.  HiWord);
  • void DWordToWords(int x, short *lo, short *hi)
  • The "const" is useless. "short" is conventional for "short int". The
  • prototype is also easier to read if you use shorter variable names (a bit of
  • a matter of taste, this).
  •  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;
  • short LOWord = 0;
  • short HiWord = 0;
  •  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;
  •  }
  • This function relies on shorts being 16 bits and ints being 32. You could
  • just write
  • *LoWord = DWord & 0xFFFF;
  •  void MakeDWord(const short LoWord, const short HiWord, int* DWord)
  •  {
  •  *DWord = HiWord<<16;
  •  *DWord |= LoWord;
  •  }
  • Same thing applies to this function.
  •  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));
  •  }
  • Assuming these functions work you could use them to implement a more
  • portable dword to words function.
  •  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);
  •  }
  • What do you intend to use this one for?
  •  

  • Nov 14 '05 #2
    On 19 Jun 2004 14:31:00 -0700, es*********@rediffmail.com (Curious
    Student) wrote in comp.lang.c:

    There are some very serious dependencies on types here.
    I'd be obliged if someone could validate the functions I've written
    below from the try-it-yourself excercises of K&R:

    Expand|Select|Wrap|Line Numbers
    1.  #include <stdio.h>
    2.  #include <stdlib.h>
    3.  #include <string.h>
    4.  void DWordToWords(const int DWord, short int* LoWord, short int*
    5.  HiWord);
    6.  void MakeDWord(const short LoWord, const short HiWord, int* DWord);
    7.  unsigned GetBits(unsigned, int, int);
    8.  unsigned SetBits(unsigned, int, int, int);
    9.  unsigned Invert(unsigned, int, int);
    10.  /*int main(void)
    11.  {
    12.      int DWord;
    13.      short LoWord, HiWord;
    14.      DWord = 1066825727;
  •  
  • There is no guarantee that this value can be represented in an int.
  • The C standard requires that an int be able to represent values in the
  • range of -32767 to +32767.
  •  
  • There is also no guarantee in the C standard that an int has any
  • greater range of values than a short int.
  •      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);
  •  
  • On a platform with 16 bit ints, this shift overflows and produces
  • undefined behavior.
  •      *HiWord = DWord>>16;
  •  
  • As does this one.
  •  }
  •  void MakeDWord(const short LoWord, const short HiWord, int* DWord)
  •  {
  •      *DWord = HiWord<<16;
  •  
  • In this case, the short HiWord is promoted to int and left shifted 16
  • places.  Again, if int has 16 bits the behavior is undefined.
  •      *DWord |= LoWord;
  •  }
  •  
  • You don't call the next three functions in this program but they are
  • all extremely dangerous if passed improper values of p and n.  If the
  • expression "p-n+1" evaluates to less than 0, or greater than the width
  • of an int in bits, you have undefined behavior again.
  •  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);
  •  }
  •  


  • --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
    Nov 14 '05 #3

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    4
    by: frogman042 | last post by:
    My daughter is playing around trying to learn JavaScript and she wrote a small program that prints out a message in increasing and decreasing font size and color changes. She is using document...
    3
    by: dataguy | last post by:
    I can't find it anywhere in the manual, but I have a developer that wants to know if we can code a trigger to capture the data that has changed in a table only if certain columns have changed. It...
    7
    by: sara | last post by:
    I have a main form for a little application and I want the Main Form to open with NONE of the buttons having focus. Now, the first button has focus. It's just a plain form, with buttons...
    11
    by: Sharon | last post by:
    I'm writing a new control derived from UserControl. I need to get an event when the control is done resizing. I tried the Resize, SizeChanged, Move and the Layout events and I also tried to...
    6
    by: Gordowey | last post by:
    Where can I find this DIV effect. go to: http://www.amazon.com Put mouse over the text "See All 32 Product Categories" (Top-Middle)..it will open a window with a cool effect... does anyone...
    4
    by: axlq | last post by:
    Suppose I had to lay out a part of a page where I wanted to retain the horizontal and vertical relationship of the elements. Yes, this is what tables are for. I was curious if it can be done in...
    2
    by: maya | last post by:
    http://news.yahoo.com/news?tmpl=index2&cid=703 down the page, under "More Stories", there's a section with two interchangeable divs which slide back and forth into view.. how is this done? I...
    19
    by: zzw8206262001 | last post by:
    Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
    10
    by: =?Utf-8?B?UHVjY2E=?= | last post by:
    Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
    40
    by: Dave | last post by:
    Hello, I'm teaching myself C by working my way through Steve Summit's tutorial (http://www.eskimo.com/~scs/cclass/cclass.html). In one of the questions (assignment 6, exercise 7), you have to...
    1
    by: CloudSolutions | last post by:
    Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
    0
    by: ryjfgjl | last post by:
    In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
    0
    by: taylorcarr | last post by:
    A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: aa123db | last post by:
    Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
    0
    by: ryjfgjl | last post by:
    If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
    0
    by: ryjfgjl | last post by:
    In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.