Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication17.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- void do_binar(unsigned int i, int tab[32]);
- void dawaj_bita(unsigned int &bity, int nrbitu, int ktorybit)
- {
- unsigned int irob;
- if (nrbitu > 31)
- {
- cout << "funkcja dawaj_bita: numer bitu musi byc mniejszy";
- cout << "koniec....press any key...." << endl;
- getchar();
- getchar();
- exit(0);
- };
- irob = 1;
- irob = irob << nrbitu;
- if (ktorybit == 0)
- {
- bity = bity&(~irob);
- }
- else if (ktorybit == 1)
- {
- bity = bity | irob;
- }
- else
- {
- cout << "funkcja dawaj_bita: bit moze byc 0 albo 1" << endl;
- cout << "koniec....press any key...";
- getchar();
- getchar();
- exit(0);
- }
- }
- void neguj_bit(unsigned int &bity, int nrbitu)
- {
- unsigned int irob;
- irob = 1;
- irob = irob << nrbitu;
- bity = bity^irob;
- }
- int main()
- {
- unsigned int ibity;
- int nrbitu, iwart, ispr;
- int tab[32];
- // cout <<"rozmiar"<< sizeof(unsigned int) << endl;
- cout << "wpisywanie bitow:" << endl;
- cout << "podaj bity jako liczbe calkowita ";
- cin >> ibity;
- do_binar(ibity, tab);
- for (int ii = 31; ii >= 0; ii--)
- {
- cout << " " << tab[ii];
- if (ii % 8 == 0)cout << " ";
- }
- cout << endl;
- //ustaw_bit = (u mnie) dawaj_bita
- cout << "podaj numer bitu: " << endl;
- cin >> nrbitu;
- cout << "podaj wartosc bitu: " << endl;
- cin >> iwart;
- dawaj_bita(ibity, nrbitu, iwart);
- cout << "nowa wartosc: " << endl << endl;
- do_binar(ibity, tab);
- for (int ii = 31; ii >= 0; ii--)
- {
- cout << " " << tab[ii];
- if (ii % 8 == 0) cout << " ";
- }
- cout << endl;
- //odwracanie bitu
- cout << "podaj numer bitu: " << endl;
- cin >> nrbitu;
- neguj_bit(ibity, nrbitu);
- cout << "wartosc po negacji" << endl << endl;
- do_binar(ibity, tab);
- for (int ii = 31; ii >= 0; ii--)
- {
- cout << " " << tab[ii];
- if (ii % 8 == 0) cout << " ";
- }
- cout << endl;
- getchar();
- getchar();
- return 0;
- }
- void do_binar(unsigned int i, int tab[32])
- {
- unsigned int ilicz, irob;
- irob = i;
- for (ilicz = 0; ilicz <= 31; ilicz++)
- {
- tab[ilicz] = irob % 2;
- irob /= 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement