Use masks to select or set a particular flag bit : binary bit « Data Types « C++ Tutorial

C++ Tutorial
1. Language Basics
2. Data Types
3. Operators statements
4. Array
5. Development
6. Exceptions
7. Function
8. Structure
9. Class
10. Operator Overloading
11. Pointer
12. File Stream
13. template
14. STL Introduction
15. string
16. vector
17. list
18. bitset
19. set multiset
20. valarray
21. queue stack
22. deque
23. map multimap
24. STL Algorithms Modifying sequence operations
25. STL Algorithms Non modifying sequence operations
26. STL Algorithms Binary search
27. STL Algorithms Sorting
28. STL Algorithms Merge
29. STL Algorithms Min Max
30. STL Algorithms Iterator
31. STL Algorithms Heap
32. STL Algorithms Helper
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C++ Tutorial » Data Types » binary bit 
2. 10. 5. Use masks to select or set a particular flag bit
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setfill;
using std::setw;

int main() {
  unsigned long flags = 0xFF;          // Flags variable
  unsigned long bit1mask = 0x1;        // Selects bit 1
  unsigned long bit6mask = 0x20;       // Selects bit 6
  unsigned long bit20mask = 0x80000;   // Selects bit 20
  
  cout << "\nSelect bit 1 from flags    : " << setw(8<< (flags & bit1mask);
  cout << "\nSelect bit 6 from flags    : " << setw(8<< (flags & bit6mask);
  cout << "\nSwitch off bit 6 in flags  : " << setw(8<< (flags &= ~bit6mask);

  cout << "\nSwitch on bit 20 in flags  : " << setw(8<< (flags |= bit20mask);

  cout << endl;

  return 0;
}
Select bit 1 from flags    :        1
Select bit 6 from flags    :       32
Switch off bit 6 in flags  :      223
Switch on bit 20 in flags  :   524511
2. 10. binary bit
2. 10. 1. Printing an unsigned integer in bits
2. 10. 2. Display the bits within a byte.
2. 10. 3. Bitwise operators: Complement, AND, OR
2. 10. 4. Bitwise operator: ^
2. 10. 5. Use masks to select or set a particular flag bit
ww___w___._j_ava__2__s_._c_o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.