fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool Get(int Number, int Index)
  5. {
  6. return (Number & 1<<Index);
  7. }
  8.  
  9. int Set(int Number, int Index, int type)
  10. {
  11. if (type)
  12. return (Number | 1<<Index);
  13. else return (Number & ( 255 - (1<<Index) ));
  14. }
  15.  
  16. int main()
  17. {
  18. cout<<Get(158,5)<<" "<<Set(158,5,0)<<" "<<Set(158,5,1);
  19. // 158 100111100
  20. // Get(158,5) lay bit o vi tri thu 5 ==> ket qua: bit 0
  21. // Set(158,5,0) doi bit o vi tri thu 5 thanh bit 0 ==> ket qua 158
  22. // Set(158,5,1) doi bit o vi tri thu 5 thanh bit 1 ==> ket qua 190 (101111100)
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
0 158 190