#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Display (short A, short B);
void ToBinary (char * Display, short N);
short main () {
time_t T;
srand (time (&T));
Display (2, 7);
Display (15, 22);
Display (rand() - 16768, rand() - 16768);
Display (rand() - 16768, rand() - 16768);
return 0;
}
void Display (short I, short J) {
char Binary [17];
short K;
K = I & J;
ToBinary (Binary, I);
printf (" %6i %s\n", I, Binary);
ToBinary (Binary, J);
printf ("& %6i %s\n", J, Binary);
printf (" ------ ----------------\n");
ToBinary (Binary, K);
printf (" %6i %s\n", K, Binary);
printf (" ====== ================\n\n");
}
void ToBinary (char * Display, short N) {
short I;
I = 15;
while (I >= 0) {
Display [I] = (N & 1) + '0';
N >>= 1;
--I;
}
Display [16] = 0;
}
#include <stdio.h>
#include <stdlib.h>
void Pack (int * Code, unsigned month, unsigned day, int year);
void Unpack (int Code, unsigned * Month, unsigned * Day, int * Year);
int main () {
int year;
unsigned month;
unsigned day;
int Packed;
year = 1900 + (rand () % 200);
month = 1 + rand () % 12;
day = 1 + rand () % 28;
printf ("Initial: %i/%u/%u\n", month, day, year);
Pack (&Packed, month, day, year);
printf ("Packed: %il\n", Packed);
Unpack (Packed, &month, &day, &year);
printf ("Unpacked: %i/%u/%u\n", month, day, year);
return 0;
}
void Pack (int * Code, unsigned Month, unsigned Day, int Year) {
*Code = Year << 9
| (Month & 15) << 5
| (Day & 31);
}
void Unpack (int Code, unsigned * Month, unsigned * Day, int * Year) {
*Year = Code >> 9;
*Month = (Code >> 5) & 15;
*Day = Code & 31;
}