CS253

CS253: Software Development with C++

Spring 2018

Bit Manipulation

See this page as a slide show

Bit Manipulation

CS253 Bit Manipulation

Refresher

C++ can do bit manipulation:

Displaying values in other bases

// Sorry, no bin
cout << (127 & 0xfe) << '\n';
cout << hex << (127 & 0xfe) << '\n';
cout << oct << (127 & 0xfe) << '\n';
126
7e
176

I/O manipulators such as hex and oct are “sticky”; they persist until changed.

Precedence matters

cout << (1|2) << '\n';
3

However, the binary bitwise operators are low precedence:

cout << 1|2 << '\n';
c.cc: In function 'int main()':
c.cc:1:10: error: no match for 'operator|' in '
   std::cout.std::basic_ostream<_CharT, _Traits>::operator<< <char, 
   std::char_traits<char> >(1) | (2 << 10)' (operand types are 
   'std::basic_ostream<char>' and 'int')
c.cc:1:10: note: candidate: operator|(int, int) <built-in>
c.cc:1:10: note:   no known conversion for argument 1 from 
   'std::basic_ostream<char>' to 'int'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40:0,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:87:3: note: candidate: 
   constexpr std::_Ios_Fmtflags std::operator|(std::_Ios_Fmtflags, 
   std::_Ios_Fmtflags)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:87:3: note:   no known 
   conversion for argument 1 from 'std::basic_ostream<char>' to 
   'std::_Ios_Fmtflags'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:129:3: note: candidate: 
   constexpr std::_Ios_Openmode std::operator|(std::_Ios_Openmode, 
   std::_Ios_Openmode)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:129:3: note:   no known 
   conversion for argument 1 from 'std::basic_ostream<char>' to 
   'std::_Ios_Openmode'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:169:3: note: candidate: 
   constexpr std::_Ios_Iostate std::operator|(std::_Ios_Iostate, 
   std::_Ios_Iostate)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:169:3: note:   no known 
   conversion for argument 1 from 'std::basic_ostream<char>' to 
   'std::_Ios_Iostate'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:39:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bitset:1434:5: note: candidate: 
   template<long unsigned int _Nb> std::bitset<_Nb> std::operator|(const 
   std::bitset<_Nb>&, const std::bitset<_Nb>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bitset:1434:5: note:   template argument 
   deduction/substitution failed:
c.cc:1:16: note:   'std::basic_ostream<char>' is not derived from 'const 
   std::bitset<_Nb>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_atomic.h:33:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:83,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/atomic_base.h:74:3: note: candidate:
   constexpr std::memory_order std::operator|(std::memory_order, 
   std::__memory_order_modifier)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/atomic_base.h:74:3: note:   no 
   known conversion for argument 1 from 'std::basic_ostream<char>' to 
   'std::memory_order'

Setting a bit

int n = 32;
cout << hex << n << '\n';
cout << hex << (n|02) << '\n';
20
22

Or, using assignment:

int n = 32;
cout << hex << n << '\n';
n |= 02;
cout << hex << n << '\n';
20
22

Clearing bits

cout << hex << 126 << '\n';
cout << hex << (126 & ~0xF) << '\n';
7e
70

Testing bits

for (char c='A'; c<'N'; c++)
    if (c & 0x01)
        cout << "'" << c << "' (" << int(c) << ") is odd.\n";
    else
        cout << "'" << c << "' (" << int(c) << ") is even.\n";
'A' (65) is odd.
'B' (66) is even.
'C' (67) is odd.
'D' (68) is even.
'E' (69) is odd.
'F' (70) is even.
'G' (71) is odd.
'H' (72) is even.
'I' (73) is odd.
'J' (74) is even.
'K' (75) is odd.
'L' (76) is even.
'M' (77) is odd.

Shifting

int n = 42;
cout << oct << n << '\n';
n >>= 1;
cout << n << '\n';
n >>= 2;
cout << n << '\n';
n >>= 1;
cout << n << '\n';
n >>= 1;
cout << n << '\n';
n >>= 1;
cout << n << '\n';
52
25
5
2
1
0

Modified: 2017-12-21T19:56

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building