Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <conio.h>
- #include <cstdlib>
- using namespace std;
- void print_bits (unsigned n)
- {
- int bit;
- for (bit = sizeof(n)*8-1; bit >= 0; bit--)
- {
- if ((n >> bit) & 01)
- {
- putchar ('1');
- }
- else
- {
- putchar ('0');
- }
- if (bit % 4 == 0) putchar (' ');
- }
- cout<<endl;
- }
- unsigned replace_bits (unsigned x, unsigned p, unsigned n)
- {
- unsigned mnoj=0, z;
- int bit;
- for (bit = sizeof(n)*8-1; bit >= 0; bit--)
- {
- if (bit>=p && bit<p+n)
- {
- z=((~(x>>bit) & 01));
- mnoj=(mnoj<<1) | z;
- }
- else
- {
- z=(((x>>bit) & 01));
- mnoj=(mnoj<<1) | z;
- }
- }
- cout<<endl;
- return mnoj;
- }
- main ()
- {
- unsigned num;
- cout << "Input X" << endl;
- cin >> num;
- print_bits (num);
- int n,p;
- do
- {
- cout << " Enter P [numeracia s nula]: " ;
- cin >> p;
- if (p<0) {cout<<" -> ERROR : P must be >=0"<<endl;}
- } while (p<0);
- do
- {
- cout << " Enter N : " ;
- cin >> n;
- if (n<=0) {cout<<" -> ERROR : N must be >=1"<<endl;}
- } while (n<=0);
- unsigned result = replace_bits (num,p,n);
- print_bits (result);
- system ("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement