ex3_03.cpp

来自「一本语言类编程书籍」· C++ 代码 · 共 25 行

CPP
25
字号
// Exercise 3.3 Using a bitwise operator to affect the binary representation of an integer.
// The simplest way to invert the last bit of a number is to exclusive-OR the number with 1.
// The effect of changing the value of the last bit may depend on your computer,
// but on mine this program adds 1 to an even integer, and subtracts 1 from an odd integer.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main() {
  const int maskValue = 1;

  int value = 0;
  int result = 0;

  cout << "Please enter an integer value: ";
  cin >> value;

  result = value ^ maskValue;
  cout << "The value of " << value << " with its lowest bit inverted is " 
       << result << endl;

  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?