| | |
Exercise using: unsigned int datecode(int year, int month, int day);
![]() |
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:
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:
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
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)!
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:
C++ Syntax (Toggle Plain Text)
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[80], value[80]; 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]; }
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
•
•
•
•
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)!
>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:
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:
C++ Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
@ 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
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
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:
use it like this:
if you just need 16 bits then use unsigned short instead of unsigned int. Also include the <climits> header and <cstdlib>
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
bit_print(datecode(int day))
>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?
Why must everyone do things the hard way?
C++ Syntax (Toggle Plain Text)
#include <bitset> #include <iostream> #include <string> using namespace std; int main() { cout<< bitset<16> ( 2685 ).to_string() <<endl; }
I'm here to prove you wrong.
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:
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
Thanks for the example anyway
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:
C++ Syntax (Toggle Plain Text)
bit_print(datecode(int day))
>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
C++ Syntax (Toggle Plain Text)
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
•
•
•
•
Originally Posted by Narue
>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?
C++ Syntax (Toggle Plain Text)
#include <bitset> #include <string> cout<< bitset<16> ( 2685 ).to_string() <<endl;
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:
•
•
•
•
Why does it have to be written like that, since you don't use the datecode function?
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: need to find occurances in a string
- Next Thread: C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






