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:

``````unsigned int datecode (int day);

int main()
{
int  day = 3, value;

value = datecode (day);

cout<< "The coded value equales: " << value <<endl;

cin.get();

return 0;
}

unsigned int datecode (int day)
{
int remain = 0, k = 0, n = 0;
int temp, value;

do
{
remain  = day % 2;
day  = day / 2;   // whittle down decimal
temp[k++] = remain + 0x30; // makes characters 0 or 1
}
while (day > 0);

while (k >= 0)
{
value[n++] = temp[--k];   // reverse spelling
}
value[n-1] = 0;

return value[n];
}``````

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 ;)

## All 13 Replies

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?

>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:

``````#include <iostream>

int main()
{
int day = 29;
int month = 3;
int year = 5;
int coded = 0;

coded = year; // Put the year in the low order bits
coded <<= 4; // Shift the year by 4 bits to make room for the month
coded |= month; // Put the month in the low order bits
coded <<= 5; // Shift the year and month by 5 bits to make room for the day
coded |= day; // Put the day in the low order bits and you're done

std::cout<< coded <<std::endl;
}``````

@ 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 :D

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:

``````void bit_print(unsigned short a)
{
int i;
int n = sizeof(short) * CHAR_BIT;
int mask = 1 << (n -1);

for(i = 1; i <= n; ++i)
{
putchar(((a & mask) == 0)? '0':'1');
a <<= 1;
if( i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
}``````

use it like this:

``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>

>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?

``````#include <bitset>
#include <iostream>
#include <string>

using namespace std;

int main()
{
cout<< bitset<16> ( 2685 ).to_string() <<endl;
}``````

;)

Why must everyone do things the hard way?

Because C is still out there along side C++ ;)

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:

``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 :?:

``````void bit_print(unsigned short a);

int main()
{
int  day = 3;

cout<< "The coded value equales: ";
bit_print(day);

cin.get();

return 0;
}

void bit_print(unsigned short a)
{
int i;
int n = sizeof(short) * CHAR_BIT;
int mask = 1 << (n -1);

for(i = 1; i <= n; ++i)
{
putchar(((a & mask) == 0)? '0':'1');
a <<= 1;
if( i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
}``````

Thanks for the example anyway :D

>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?

``````#include <bitset>
#include <string>

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:

Oh yes, it works like a charm your tinny piece of code, well, if you can still call it writing code :lol:

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.

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.

Aha, now I understand :!:

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.

It must be iostream that does it since I didn't include any other headerfile.

Thanks for the help :!:

>Because C is still out there along side C++
And if you're using C++ then you should take advantage of it. If the C solution is used because "C is still out there along side C++" then why use C++ in the first place? I'm sorry, but I find that excuse lacking.

>Is bitset a headerfile wich exists
bitset is a standard template class defined in the <bitset> header. It's handy every now and then, but for the most part it's not a commonly used class from the standard library.

>It must be iostream that does it since I didn't include any other headerfile.
Don't rely on that behavior though. When you use a something from the standard library, you should include the correct header for your code to be valid on every compiler.

I'm sorry, but I find that excuse lacking.

Here's another: It will help one understand how to do it manually, increasing one's knowledge.

>Here's another: It will help one understand how to do it manually, increasing one's knowledge.
That's more like what I was expecting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.