关于~和-的区别

本文通过一系列示例代码展示了在一元取负运算符(-)和按位取反运算符(~)作用下整数的变化,揭示了两者在计算机内部表示上的不同之处。文章还提供了一些练习题供读者进一步理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

~和-这两个运算符看起来是那么相似,感觉是一种互逆的运算符(比如一元+和医院-,取地址&和解引用*...)

但是,如果你认为它们是相等的,那就不一定是正确的,先请看下面这一段例子.

Code:
  1. #include <iostream>  
  2.   
  3. int main()  
  4. {  
  5.     std::cout << -3  << std::endl;  
  6.     std::cout << ~3 << std::endl;  
  7.     std::cout << -~3 << std::endl;  
  8.     std::cout << ~-~3 << std::endl;  
  9.     std::cout << -~-3 << std::endl;  
  10.     std::cout << ~-~-3 << std::endl;  
  11.     std::cout << -~-~3 << std::endl;  
  12.     std::cout << ~-~-~3 << std::endl;  
  13.     std::cout << -~-~-3 << std::endl;  
  14.     return 0;  
  15. }  

如果请您写出这段代码的结果,你能保证完全正确吗?

如果不能保证完全正确,那就请继续讲本文看下去.

一开始我们先进入那二进制世界来讨论,首先取负运算符是按照计算机中补码的原理进行求反,所以不管怎么变,它的绝对值都不变.

但是按位取反运算符就不一样了,它并不会像取负运算符那样是按照补码原理进行求反的,它直接就是一个一个位进行01互换,而没有补码中的01互换后还要在末尾加1.弄清楚这一点非常重要,这对我们例子中的结果有极大的关系.

回到我们的例子中来.

它的运行结果如下:

Code:
  1. -3  
  2. -4  
  3. 4  
  4. -5  
  5. -2  
  6. 1  
  7. 5  
  8. -6  
  9. -1  

内容已经讲完了,下面再给几道练习题

Code:
  1. // 下面是练习题  
  2. ~~~~-~~-~5;  
  3. -~---~~~-~~19;  
  4. ~--~~~-~--~2;  
  5. ~~~--~~---4;  
  6. --~--~--~--~14;  
  7. ---~---~----~~~32;  

请如果要做题的同学们请自己算,如果借助编译器,则不能达到提升的效果.

 

catkin_make Base path: /home/hhb/catkin_ws Source space: /home/hhb/catkin_ws/src Build space: /home/hhb/catkin_ws/build Devel space: /home/hhb/catkin_ws/devel Install space: /home/hhb/catkin_ws/install #### #### Running command: "make cmake_check_build_system" in "/home/hhb/catkin_ws/build" #### -- Using CATKIN_DEVEL_PREFIX: /home/hhb/catkin_ws/devel -- Using CMAKE_PREFIX_PATH: /home/hhb/catkin_ws/devel;/opt/ros/noetic -- This workspace overlays: /home/hhb/catkin_ws/devel;/opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/hhb/catkin_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.10 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on WARNING: package "turtlebot3_gazebo" should not depend on metapackage "gazebo_ros_pkgs" but on its packages instead -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 22 packages in topological order: -- ~~ - turtlebot3_simulations (plain cmake) -- ~~ - wpb_home_bringup -- ~~ - wpb_home_remote -- ~~ - abc_swarm -- ~~ - map_pkg -- ~~ - slam_pkg -- ~~ - tianbot_mini -- ~~ - nav_pkg -- ~~ - lidar_pkg -- ~~ - multi_robot_slam -- ~~ - multirobot_map_merge -- ~~ - turtlebot3_fake_node (plain cmake) -- ~~ - tianbot_mini_description -- ~~ - turtlebot3_gazebo (plain cmake) -- ~~ - explore_lite -- ~~ - rrt_star_global_planner -- ~~ - wpb_home_behaviors -- ~~ - wpb_home_python -- ~~ - wpb_home_python3
03-08
WARNING: Package name "Send_position_pose" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes. WARNING: Package name "Send_position_pose" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 4 packages in topological order: -- ~~ - Send_position_pose -- ~~ - my_odometry_listener -- ~~ - livox_ros_driver2 -- ~~ - fast_lio -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'Send_position_pose' -- ==> add_subdirectory(Send_position_pose) CMake Deprecation Warning at Send_position_pose/CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy WARNING: Package name "Send_position_pose" does not follow the naming conventions. It should start with a lower case letter and only contain lower case letters, digits, underscores, and dashes. CMake Error at /opt/ros/noetic/share/catkin/cmake/catkin_package.cmake:196 (message): catkin_package() the catkin package 'tf2' has been find_package()-ed but is not listed as a build dependency in the package.xml Call Stack (most recent call first): /opt/ros/noetic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_package) Send_position_pose/CMakeLists.txt:210 (catkin_package) -- Configuring incomplete, errors occurred! make: *** [Makefile:1892:cmake_check_build_system] 错误 1 Invoking "make cmake_check_build_system" failed
07-19
### #### Running command: "cmake /home/rosnoetic/demo01_ws/src -DCATKIN_DEVEL_PREFIX=/home/rosnoetic/demo01_ws/devel -DCMAKE_INSTALL_PREFIX=/home/rosnoetic/demo01_ws/install -G Unix Makefiles" in "/home/rosnoetic/demo01_ws/build" #### -- Using CATKIN_DEVEL_PREFIX: /home/rosnoetic/demo01_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic -- This workspace overlays: /opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/rosnoetic/demo01_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 1 packages in topological order: -- ~~ - helloworld -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'helloworld' -- ==> add_subdirectory(helloworld) -- Could NOT find std_msg (missing: std_msg_DIR) -- Could not find the required component 'std_msg'. The following CMake error indicates that you either need to install the package with the same name or change your environment so that it can be found. CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package): Could not find a package configuration file provided by "std_msg" with any of the following names: std_msgConfig.cmake std_msg-config.cmake Add the installation prefix of "std_msg" to CMAKE_PREFIX_PATH or set "std_msg_DIR" to a directory containing one of the above files. If "std_msg" provides a separate development package or SDK, be sure it has been installed. Call Stack (most recent call first): helloworld/CMakeLists.txt:10 (find_package) -- Configuring incomplete, errors occurred! See also "/home/rosnoetic/demo01_ws/build/CMakeFiles/CMakeOutput.log". See also "/home/rosnoetic/demo01_ws/build/CMakeFiles/CMakeError.log". Invoking "cmake" failed rosnoetic@rosnoetic-VirtualBox:~/demo01_ws$ conda deactivate conda: command not found rosnoetic@rosnoetic-VirtualBox:~/demo01_ws$ echo $ROS_PACKAGE_PATH # /opt/ros/noetic/share rosnoetic@rosnoetic-VirtualBox:~/demo01_ws$
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值