Skip to content

Instantly share code, notes, and snippets.

@sssgun
Created March 29, 2018 07:16
Show Gist options
  • Save sssgun/6064ff9db8860cdafb9905495eacc113 to your computer and use it in GitHub Desktop.
Save sssgun/6064ff9db8860cdafb9905495eacc113 to your computer and use it in GitHub Desktop.
shift operation example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#define MAX_LOOPS 2000000
#define MAX_THREAD_COUNT 100
int shift_01()
{
#ifdef USED_THREAD
int i = 2;
#else
int i;
printf("정수를 입력하세요 : ");
scanf("%d", &i);
#endif
//printf("%d 를 32 로 나누면 : %d \n", i, i/32);
printf("%d 를 1000 로 나누면 : %d \n", i, i/1000);
printf("%d 를 5 칸 쉬프트 하면 : %d %d\n", i, i >> 5, i << 5);
printf("%d 를 4 칸 쉬프트 하면 : %d %d\n", i, i >> 4, i << 4);
printf("%d 를 3 칸 쉬프트 하면 : %d %d\n", i, i >> 3, i << 3);
printf("%d 를 2 칸 쉬프트 하면 : %d %d\n", i, i >> 2, i << 2);
printf("%d=(%d/%d), %d\n", (int)((15 * 30 + i) / 16), (15 * 30 + i), 16, (int)((15 * 30 + i) >> 4));
return 0;
}
int shift_02()
{
printf("%f, %f\n", ( 1.0f / 128.0f ), ((1000000>>7)*0.000001f));
printf("%f, %f\n", ( 1.0f / 256.0f ), ((1000000>>8)*0.000001f));
printf("%f, %f\n", ( 1.0f / 32768.0f ), ((100000000>>15)*0.00000001f));
printf("%f, %f\n", ( 1.0f / 65536.0f ), ((100000000>>16)*0.00000001f));
return 0;
}
int shift_03()
{
#ifdef USED_THREAD
int i = 100;
#else
int i;
printf("정수를 입력하세요 : ");
scanf("%d", &i);
#endif
printf("%d, %d\n", i, (i & 0xFFFC));
printf("%d, %d, %d, %d\n", i*i, i << 1, i << 2, i << 3);
return 0;
}
int shift_04()
{
int i, m = 100;
for (i = 0; i < m; i++) {
printf("%f, %f\n", ( (1.0f * i) / 128.0f ), (((i * 1000000)>>7)*0.000001f));
}
return 0;
}
int shift_05()
{
double x = 2.0, y;
int p = 3;
y = ldexp( x, p );
printf( "%2.1f times two to the power of %d is %2.1f, %2.1f\n", x, p, y, (float)(x * (1 << p)) );
return 0;
}
void *consumer(void *ptr)
{
#ifdef USED_THREAD
int type = 4;
#else
int type = (int)*(int *)(ptr);
#endif
#if 0
int i = 0;
for (i = 1; i < 10; i++) {
printf("%d ", 2 << i);
}
printf("\n");
#endif
switch (type) {
case 1:
shift_01();
break;
case 2:
shift_02();
break;
case 3:
shift_03();
break;
case 4:
shift_04();
break;
case 5:
shift_05();
break;
default:
break;
}
return;
}
int main(int argc, const char *const argv[])
{
int i, j, loop = 0;
pthread_t thr[MAX_THREAD_COUNT];
struct timeval tv1, tv2;
if (argc > 1) {
loop = atoi(argv[1]);
} else {
loop = 1;
}
// Measuring time before starting the threads...
gettimeofday(&tv1, NULL);
//for (j=0; j < loop; j++) {
#ifdef USED_THREAD
for (i=0; i < MAX_THREAD_COUNT; i++) {
pthread_create(&thr[i], NULL, consumer, NULL);
}
for (i=0; i < MAX_THREAD_COUNT; i++) {
pthread_join(thr[i], NULL);
}
#else
consumer(&loop);
#endif
//}
// Measuring time after threads finished...
gettimeofday(&tv2, NULL);
if (tv1.tv_usec > tv2.tv_usec) {
tv2.tv_sec--;
tv2.tv_usec += 1000000;
}
printf("Result - %ld.%ld\n", tv2.tv_sec - tv1.tv_sec, tv2.tv_usec - tv1.tv_usec);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment