Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- int *binary(int);
- void display(int *);
- int check_pos(int,int);
- int get_val(int *,int,int);
- int n,arr[8],ii=0,_bin[8],j = 0,_count=-1;
- int main()
- {
- int x, *p1, *p2, *p3, *p4;
- printf("Int Memory data:\n");
- scanf("%d", &x);
- printf("4th : \t");
- p4 = binary(*((char *)(&x) + 3));
- display(p4);
- (x < 0) ?
- printf(" Value %u",get_val(p4,(j-1),4)) // for -ve numbers 2's compliment
- ://:
- printf(" Value %d",get_val(p4,(j-1),4))
- ;//;
- // for +ve numbers
- printf("\n3rd : \t");
- p3 = binary(*((char *)(&x) + 2));
- display(p3);
- printf(" Value %d",
- get_val(p3,(j-1),3));
- printf("\n2nd : \t");
- p2 = binary(*((char *)(&x) + 1));
- display(p2);
- printf(" Value %d",
- get_val(p2,(j-1),2));
- printf("\n1st : \t");
- p1 = binary(*((char *)(&x) + 0));
- display(p1);
- printf(" Value %d",
- get_val(p1,(j-1),1));
- return 0;
- }
- int* binary(int n)
- {
- int c, k;
- j = 0;// reset bit -_-
- for (c = 7; c >= 0; c--)
- {
- k = n >> c;
- if (k & 1)
- _bin[j++] = 1;
- else
- _bin[j++] = 0;
- }
- return _bin;
- }
- void display(int *p)
- {
- int i;
- for (i = 0; i < j; i++)
- printf("%d", *(p + i));
- }
- int check_pos(int pos, int call)
- {
- switch (call)
- {
- case 1:
- return (int)pow(2, (0 + pos));
- break;
- case 2:
- return (int)pow(2, (8 + pos));
- break;
- case 3:
- return (int)pow(2, (16 + pos));
- break;
- case 4:
- return (int)pow(2, (24 + pos));
- break;
- default : return (int)((void*)0);//its int NULL o_O
- }
- }
- int get_val(int *p,int _j,int _c)
- {
- static int _sum;
- //Reset bits -_-
- _count = -1;_sum = 0;
- while (_j >= 0)
- {
- ++_count;// incriment _count every time
- if (p[_j--] == 1)//check in reverse order
- _sum += check_pos(_count,_c);
- }
- return _sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement