#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <conio.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
// --------------------------------------------------------

void skip(int no)
{
	int i;
	
	// limits no in 1 .. 20
	if ((no >= 1) && (no <= 20)) {// OK, no is OK
		for (i=1;i<=no;i++) {
			// printf("i= %2d \n", i);
			printf("\n");
		}
	}
	else {// no Good!
		// printf("\n no Good! \n");
		printf("\n");
	}
}// end of skip()
// --------------------------------------------------------

//   pause();
void pause(void)
{
	// remove type- ahead
	while (kbhit()) {
		getch();
	}
	
	printf("Press [Esc] for stop! others for Continue...");
	do {
		// wait for keyPressed
	} while (!kbhit());
	printf("\n");
	
	int ch1= getch();
	if (ch1==0x1b) {
		exit(1);
	}
	
	// remove extra keyStroke
	while (kbhit()) {
		getch();
	}
}// end of pause()
// --------------------------------------------------------


// #include "sj-02.h"
//   time1(    &t1);
void time1(int *t1)
{
	int s1, ms;
	struct _timeb timebuffer;
	_ftime( &timebuffer );
	
	s1= timebuffer.time;
	ms= timebuffer.millitm;
	
	s1%= (1L*24*60*60);
	(*t1)= s1*1000 + ms;
}// end of time1()
// --------------------------------------------------------

//   time2(t1, &dt);
void time2(int t1, double *dt)
{
	int t2;
	
	time1(&t2);
	(*dt)= (t2 - t1)/1000.0;
	
	// must (*dt) >= 0
	if ((*dt) < 0.0) {
		(*dt)+= (1.0*24*60*60);
	}
}// end of time2()
// --------------------------------------------------------



// #include "sj-03.h"
//   rnd1(    &s1);
void rnd1(int *s1)
{
	double r1= (double) (*s1);
	r1= fmod(r1*16807.0 , 2147483647.0);
	
	(*s1)= (int) (r1 + 0.5);
}// end of rnd1()
// --------------------------------------------------------

//   init_rnd(&s1);
void init_rnd(int *s1)
{
	int t1, t2, i;
	time1(&t1);
	
	t2= t1;
	while (t2 == t1) {
		time1(&t1);
	}
	// get a new t1
	
	(*s1)= t1;
	for (i=0;i<300;i++) {
		rnd1(s1);
	}
}// init_rnd()
// --------------------------------------------------------

//   rnd2(&s1, &x);
void rnd2(int *s1, double *x)
{
	rnd1(s1);
	
	(*x)= ((double) *s1)/(2147483647.0);
}// end of rnd2()
// --------------------------------------------------------


void swap_int(int *a, int *b)
{
	int temp;
	temp= *a;
	*a= *b;
	*b= temp;
	
	
}

//   irnd(&s1, i1, i2, &ii);
void irnd(int *s1, int i1, int i2, int *ii)
{
	double x;
	
	// limits i1 <= i2
	if (i1 > i2) {
		// swap2v(&i1, &i2);
		swap_int(&i1, &i2);
		
	}
	
	rnd2(s1, &x);
	(*ii)= (int) (x*(i2 - i1 + 1) + i1);
}// end of irnd()
// --------------------------------------------------------

// delay_ms(ii);
void delay_ms(int ms)
{
	int t1, t2;
	
	time1(&t1);
	t2= t1 + ms;
	
	// repeat ... until (t1 > t2);
	do {
		time1(&t1);
	} while (!(t1 > t2));
}
// --------------------------------------------------------

// ----------------------------------------------

typedef unsigned int ui;
typedef char str80[80];
// ----------------------------------------------

void dec2bin(ui a, str80 s1)
{
	int i;
	
	for (i=0;i<8;i++) {
		s1[7-i]= (a%2) + 48;
		a= a/2;
	}
	s1[8]= '\0';
}// end of dec2bin()
// ----------------------------------------------

void bin2dec(str80 s1, ui *b)
{
	int wt= 1, sum= 0, i;
	for (i=7;i>=0;i--) {
		sum+= (s1[i] - 48)*wt;
		wt*= 2;
	}
	
	*b= sum;
}// end of bin2dec()
// ----------------------------------------------

int main(int argc, char *argv[])
{
	int a, b, c, d;
	str80 s1, s2, s3, s4;
	int t1;
	
	init_rnd(&t1);
	irnd(&t1, 0, 255, &a);
	irnd(&t1, 0, 255, &b);
	
	c= a^b;
	dec2bin(a, s1);
	dec2bin(b, s2);
	dec2bin(c, s3);
	
	printf("a= %5d, s1= %8s \n", a, s1);
	printf("b= %5d, s2= %8s \n", b, s2);
	printf("c= %5d, s3= %8s \n", c, s3);
	// ------------------------------------------
	
	d= c^b;
	dec2bin(d, s4);
	printf("d= %5d, s4= %8s \n", d, s4);
    return EXIT_SUCCESS;
}// end of main()

