1. 程式人生 > >2016年12月14日學習總結----位運算程式

2016年12月14日學習總結----位運算程式

#include <stdio.h>
#include <string.h>


#define MAX_SIZE 1024


int len;


char src[MAX_SIZE];


void num_to_string(int m)
{
    int i = 0;
    int temp;


    while(m != 0)
    {
        src[i] = ! (m % 2) + '0';
i++;
m = m / 2;
    }


    src[i] = '\0';
    len = strlen(src);
    
    for(i = 0;i < len / 2;i++)
    {
        temp = src[i];
src[i] = src [len - i - 1];
src[len - i - 1] = temp;
    }
}
void str_adj(int p1,int p2,char *src)
{
    int j = 0;
    
    while(j < (p2 - p1 + 1))
    {
        src[j] = src[len - p2];
j++;
len++;
    }
    src[j] = '\0';
}
int main()
{
    int a;
    int p1;
    int p2;


    printf("please input a number:\n");
    scanf("%d",&a);
    
    num_to_string(a);


    printf("length of a(binary):%d\n",len);
    printf("please input two numbers(p1 < p2 < length):\n");
    scanf("%d %d",&p1,&p2);


    num_to_string(a);
    str_adj(p1,p2,src);


    printf("result:%s\n",src);
    return 0;
}