Exercise using: unsigned int datecode(int year, int month, int day);

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #1
Mar 29th, 2005
Hi ladies and gents,

I wanted to start a new exercise, but after reading it several times, I actually don't know what the exercise is all about :!:
The translation into English is this:
Write a function wich can be declarered as the following:

unsigned int datecode(int year, int month, int day);

The task of this function is to encode the given date into the 16 bits(the 16 right bits) of the unsigned int and deliver that as a function value. From left to right we get:
7 bits for the last two numbers of the year (<=99),
4 bits for the month (<=12)
5 bits for the day (<= 31)

Keep in account that the parameter (year) can be entered as (99) or (1999)!
Now, at first I tought that I could use the codebits wich where written by Vegaseat and Dave Sinkula, but after looking at it, it's obvious that's not the case since they do use pointers and strings :-|

Since I have to use the declaration: unsigned int datecode (int year, int month, int day);
The example of them can't be used :!:

Before I realised this, I did make this piece of code, using a huge part of Vegaseat Code Snippet: http://www.daniweb.com/code/snippet87.html
I tried to do this exercise starting with one parameter for the day wich resulted into this:
  1. unsigned int datecode (int day);
  2.  
  3. int main()
  4. {
  5. int day = 3, value;
  6.  
  7. value = datecode (day);
  8.  
  9. cout<< "The coded value equales: " << value <<endl;
  10.  
  11. cin.get();
  12.  
  13. return 0;
  14. }
  15.  
  16. unsigned int datecode (int day)
  17. {
  18. int remain = 0, k = 0, n = 0;
  19. int temp[80], value[80];
  20.  
  21. do
  22. {
  23. remain = day % 2;
  24. day = day / 2; // whittle down decimal
  25. temp[k++] = remain + 0x30; // makes characters 0 or 1
  26. }
  27. while (day > 0);
  28.  
  29. while (k >= 0)
  30. {
  31. value[n++] = temp[--k]; // reverse spelling
  32. }
  33. value[n-1] = 0;
  34.  
  35. return value[n];
  36. }
Problem with this code is:
1) The returned value isn't a piece of binary code, it's just rubbish! This because I can't use a pointer wich would contain the value of the binary code as in the original code by Vegaseat!

So, to recap a bit, my questions are:

1) Do you understand what the idea is for this exercise when reading the task?

2) If so, could you give an example of what is meant by it?

Thanks for the assistance in advance
Last edited by JoBe; Mar 29th, 2005 at 7:38 am. Reason: Changed Notification Type
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #2
Mar 29th, 2005
Write a function wich can be declarered as the following:

unsigned int datecode(int year, int month, int day);

The task of this function is to encode the given date into the 16 bits(the 16 right bits) of the unsigned int and deliver that as a function value. From left to right we get:
7 bits for the last two numbers of the year (<=99),
4 bits for the month (<=12)
5 bits for the day (<= 31)

Keep in account that the parameter (year) can be entered as (99) or (1999)!
I think the problem requires you to know what bit fields are, i.e that you know how to pack data in a single unsigned int. So you know how to use bit fields?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #3
Mar 29th, 2005
>1) Do you understand what the idea is for this exercise when reading the task?
Yes, they want you to take three integers and pack them into one integer by partitioning a certain number of bits for a value that will never exceed those bits.

>2) If so, could you give an example of what is meant by it?
If I wanted to pack 3-29-05 (today) into an int by placing the day in the low-order 5 bits, the month in the next 4 bits up, and the year in the last 7 bits, the resulting binary value would be 0000101001111101, or 2685.

This can be easily done with the bitwise operators:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int day = 29;
  6. int month = 3;
  7. int year = 5;
  8. int coded = 0;
  9.  
  10. coded = year; // Put the year in the low order bits
  11. coded <<= 4; // Shift the year by 4 bits to make room for the month
  12. coded |= month; // Put the month in the low order bits
  13. coded <<= 5; // Shift the year and month by 5 bits to make room for the day
  14. coded |= day; // Put the day in the low order bits and you're done
  15.  
  16. std::cout<< coded <<std::endl;
  17. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #4
Mar 29th, 2005
@ Asif,

Not sure about bit fields, are they 0000 = value 0(decimal)
0001 = value 1(decimal)?

@ Narue,

Thanks Narue, I tought I had to return a binary code to main and therefore was confused in how I could get binary value into an integer, as you show in your example, that's not necessary.

In fact, I could return the value 2685 and then use the code snippet from Vegaseat to turn this integer into a binary value
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #5
Mar 29th, 2005
It's rather inefficient to convert the decimal number into a string representing the binary equivalent, since the decimal number IS already in a binary form. You just need to use bit_print(), use the following function:
  1. void bit_print(unsigned short a)
  2. {
  3. int i;
  4. int n = sizeof(short) * CHAR_BIT;
  5. int mask = 1 << (n -1);
  6.  
  7. for(i = 1; i <= n; ++i)
  8. {
  9. putchar(((a & mask) == 0)? '0':'1');
  10. a <<= 1;
  11. if( i % CHAR_BIT == 0 && i < n)
  12. putchar(' ');
  13. }
  14. }

use it like this:
  1. bit_print(datecode(int day))
if you just need 16 bits then use unsigned short instead of unsigned int. Also include the <climits> header and <cstdlib>
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #6
Mar 29th, 2005
>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?
  1. #include <bitset>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. cout<< bitset<16> ( 2685 ).to_string() <<endl;
  10. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #7
Mar 29th, 2005
Why must everyone do things the hard way?
Because C is still out there along side C++
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #8
Mar 29th, 2005
Hey Asif, thanks for the additional help, tough, with the code Narue gave me it was sufficient

I did try out your code aswell, worked like a charm :!:

Still, Ive got a few questions if you don't mind

>use it like this:
  1. bit_print(datecode(int day))
Why does it have to be written like that, since you don't use the datecode function?

>Also include the <climits> header and <cstdlib>
Why do I have to include these with the use of your code, it works well without it

  1. void bit_print(unsigned short a);
  2.  
  3. int main()
  4. {
  5. int day = 3;
  6.  
  7. cout<< "The coded value equales: ";
  8. bit_print(day);
  9.  
  10. cin.get();
  11.  
  12. return 0;
  13. }
  14.  
  15. void bit_print(unsigned short a)
  16. {
  17. int i;
  18. int n = sizeof(short) * CHAR_BIT;
  19. int mask = 1 << (n -1);
  20.  
  21. for(i = 1; i <= n; ++i)
  22. {
  23. putchar(((a & mask) == 0)? '0':'1');
  24. a <<= 1;
  25. if( i % CHAR_BIT == 0 && i < n)
  26. putchar(' ');
  27. }
  28. }

Thanks for the example anyway
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #9
Mar 29th, 2005
Originally Posted by Narue
>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?
  1. #include <bitset>
  2. #include <string>
  3.  
  4. cout<< bitset<16> ( 2685 ).to_string() <<endl;
Now Narue, this is cruel :mrgreen:

I'm trying my outmost best, and you write this with only this part to be included into your code :mrgreen:

Is bitset a headerfile wich exists, or is it one you made yourself, I heard you can make your own headerfiles, so, that's why I'm asking.

Oh yes, it works like a charm your tinny piece of code, well, if you can still call it writing code :lol:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Exercise using: unsigned int datecode(int year, int month, int day);

 
0
  #10
Mar 29th, 2005
Why does it have to be written like that, since you don't use the datecode function?
the bit_print() function just prints an integer in binary. But the integer to be printed should come from the datecode() function, which Narue showed in his first post. This two functions together solves your problem that you stated in your very first post.

you should include climits because it has the value for CHAR_BIT. If it works without it probably u included something that does the same job, maybe iostream, i m not sure.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC