Advertisement
B1LLy

#9c

Dec 5th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. // ConsoleApplication17.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. void do_binar(unsigned int i, int tab[32]);
  9.  
  10. void dawaj_bita(unsigned int &bity, int nrbitu, int ktorybit)
  11. {
  12.     unsigned int irob;
  13.  
  14.     if (nrbitu > 31)
  15.     {
  16.         cout << "funkcja dawaj_bita: numer bitu musi byc mniejszy";
  17.         cout << "koniec....press any key...." << endl;
  18.             getchar();
  19.             getchar();
  20.             exit(0);
  21.     };
  22.     irob = 1;
  23.     irob = irob << nrbitu;
  24.  
  25.     if (ktorybit == 0)
  26.     {
  27.         bity = bity&(~irob);
  28.     }
  29.     else if (ktorybit == 1)
  30.     {
  31.         bity = bity | irob;
  32.     }
  33.     else
  34.     {
  35.         cout << "funkcja dawaj_bita: bit moze byc 0 albo 1" << endl;
  36.         cout << "koniec....press any key...";
  37.         getchar();
  38.         getchar();
  39.         exit(0);
  40.     }
  41. }
  42. void neguj_bit(unsigned int &bity, int nrbitu)
  43. {
  44.     unsigned int irob;
  45.  
  46.     irob = 1;
  47.     irob = irob << nrbitu;
  48.     bity = bity^irob;
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54.     unsigned int ibity;
  55.     int nrbitu, iwart, ispr;
  56.     int tab[32];
  57.    
  58.     // cout <<"rozmiar"<< sizeof(unsigned int) << endl;
  59.  
  60.     cout << "wpisywanie bitow:" << endl;
  61.     cout << "podaj bity jako liczbe calkowita  ";
  62.     cin >> ibity;
  63.     do_binar(ibity, tab);
  64.     for (int ii = 31; ii >= 0; ii--)
  65.     {
  66.         cout << " " << tab[ii];
  67.         if (ii % 8 == 0)cout << " ";
  68.     }
  69.     cout << endl;
  70.     //ustaw_bit = (u mnie) dawaj_bita
  71.     cout << "podaj numer bitu: " << endl;
  72.     cin >> nrbitu;
  73.     cout << "podaj wartosc bitu: " << endl;
  74.     cin >> iwart;
  75.     dawaj_bita(ibity, nrbitu, iwart);
  76.     cout << "nowa wartosc: " << endl << endl;
  77.     do_binar(ibity, tab);
  78.     for (int ii = 31; ii >= 0; ii--)
  79.     {
  80.         cout << " " << tab[ii];
  81.         if (ii % 8 == 0) cout << " ";
  82.     }
  83.     cout << endl;
  84.  
  85.     //odwracanie bitu
  86.     cout << "podaj numer bitu: " << endl;
  87.     cin >> nrbitu;
  88.     neguj_bit(ibity, nrbitu);
  89.     cout << "wartosc po negacji" << endl << endl;
  90.     do_binar(ibity, tab);
  91.     for (int ii = 31; ii >= 0; ii--)
  92.     {
  93.         cout << " " << tab[ii];
  94.         if (ii % 8 == 0) cout << " ";
  95.     }
  96.     cout << endl;
  97.  
  98.     getchar();
  99.     getchar();
  100.     return 0;
  101. }
  102.  
  103. void do_binar(unsigned int i, int tab[32])
  104. {
  105.     unsigned int ilicz, irob;
  106.     irob = i;
  107.     for (ilicz = 0; ilicz <= 31; ilicz++)
  108.     {
  109.         tab[ilicz] = irob % 2;
  110.         irob /= 2;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement