当前位置: 首页 > news >正文

原码补码反码与位操作

今天搜索互联网那个引擎了解了原码补码反码的相关概念
原码是最直观的编码方式,用最高位表示符号(0为正,1为负),其余位表示数值的绝对值
正数的反码与原码相同;负数的反码是对其原码的数值位取反(符号位不变)
正数的补码与原码相同;负数的补码是其反码加1
Java中的整数采用补码表示法
public class Demo{
public static void main(String[] args) {
// 定义测试用的正数和负数
int y = 13; // 二进制: 00001101
int b = -13; // 二进制: 11110011 (补码表示)

        // 打印原始数值的二进制表示System.out.println("原始数值:");printBinary("正数13", y);printBinary("负数-13", b);System.out.println();// 位与操作 (&)System.out.println("位与操作 (&):");int andResult = y & b;printOperation("13 & -13", andResult, y, b, "&");System.out.println();// 位或操作 (|)System.out.println("位或操作 (|):");int orResult = y | b;printOperation("13 | -13", orResult, y, b, "|");System.out.println();// 位异或操作 (^)System.out.println("位异或操作 (^):");int xorResult = y ^ b;printOperation("13 ^ -13", xorResult, y, b, "^");System.out.println();// 位非操作 (~)System.out.println("位非操作 (~):");int noty = ~y;int notb = ~b;printOperation("~13", noty, y);printOperation("~-13", notb, b);System.out.println();// 左移操作 (<<)System.out.println("左移操作 (<<):");int leftShiftPos = y << 2;int leftShiftNeg = b << 2;printShiftOperation("13 << 2", leftShiftPos, y, "<<", 2);printShiftOperation("-13 << 2", leftShiftNeg, b, "<<", 2);System.out.println();// 算术右移操作 (>>)System.out.println("算术右移操作 (>>):");int rightShiftPos = y >> 2;int rightShiftNeg = b >> 2;printShiftOperation("13 >> 2", rightShiftPos, y, ">>", 2);printShiftOperation("-13 >> 2", rightShiftNeg, b, ">>", 2);System.out.println();// 逻辑右移操作 (>>>)System.out.println("逻辑右移操作 (>>>):");int unsignedRightShiftPos = y >>> 2;int unsignedRightShiftNeg = b >>> 2;printShiftOperation("13 >>> 2", unsignedRightShiftPos, y, ">>>", 2);printShiftOperation("-13 >>> 2", unsignedRightShiftNeg, b, ">>>", 2);}// 打印二进制表示的辅助方法public static void printBinary(String label, int num) {String binary = String.format("%32s", Integer.toBinaryString(num)).replace(' ', '0');System.out.println(label + " (十进制:" + num + "): " + binary);}// 打印位操作结果的辅助方法public static void printOperation(String operation, int result, int operand1, int operand2, String operator) {System.out.println(operation + " = " + result);System.out.print("二进制运算: ");printBinaryShort(operand1);System.out.print(" " + operator + " ");printBinaryShort(operand2);System.out.print(" = ");printBinaryShort(result);System.out.println("\n");}// 打印位操作结果的辅助方法public static void printOperation(String operation, int result, int operand) {System.out.println(operation + " = " + result);System.out.print("二进制运算: " + operation.charAt(0) + " ");printBinaryShort(operand);System.out.print(" = ");printBinaryShort(result);System.out.println("\n");}// 打印移位操作结果的辅助方法public static void printShiftOperation(String operation, int result, int operand, String shiftOp, int shift) {System.out.println(operation + " = " + result);System.out.print("二进制运算: ");printBinaryShort(operand);System.out.print(" " + shiftOp + " " + shift + " = ");printBinaryShort(result);System.out.println("\n");}// 简短格式打印二进制public static void printBinaryShort(int num) {String binary = Integer.toBinaryString(num);if (binary.length() > 8) {binary = binary.substring(binary.length() - 8);}System.out.print(binary);}

}
手工计算结果-13的逻辑右移和算数右移与java表示结果不一样
经过搜索了解手工8位简算与java32位补码运算完全计算结果不一样,java的结算结果是正确的
我学会了要在分析位运算时,必须考虑数据类型的完整位数(int为32位),简单的8位模型会导致理解偏差。

http://www.zskr.cn/news/9803.html

相关文章:

  • 特殊句式
  • RAG系统嵌入模型怎么选?选型策略和踩坑指南
  • (应该写的比较清晰)D2. Max Sum OR (Hard Version)
  • Linux运维
  • day001
  • # Xilnx FPGA 资源结构
  • 借助S参数测量评估电容器阻抗第 2 部分
  • 实战:Android 自定义菊花加载框(带超时自动消失) - 教程
  • 超级恶心的题面 [USACO21OPEN] Portals G
  • 昆仑通态触摸屏保存参数到内部存储器并读取的方法成都控制器开发提供
  • 使用reCAPTCHA提升WordPress网站安全性 - 指南
  • LaTeX入门:10分钟掌握核心用法 - 详解
  • Codeforces 2127 D(图论,组合数学,DFS,分类讨论)
  • 每日报告-关于本学期的计划
  • 若依前后端分离版本二次开发(一 搭建开发环境,新建模块)
  • 每日博客
  • STM32HAL 飞快入门(十九):UART 编程(二)—— 中断方式实现收发及局限分析
  • 详细介绍:uniapp | u-waterfall实现瀑布流商品列表(支持筛选查询)
  • 负载分析和排查六
  • 6月6日证书 - 工信部人才交流中心PostgreSQL中级PGCP高级PGCM认证
  • 【下一款产品】
  • # MySQL索引结构发展历史:从B树到B+树的演进之路
  • 通过ML.Net调用Yolov5的Onnx模型
  • 元宇宙与零售业变革:沉浸式体验重构消费全链路 - 指南
  • c# 反射动态添加Attribute
  • MyBatis-Plus 全方位深度指南:从入门到精通
  • Stm32学习过程笔记
  • 【9.24 直播】集群数据管理实战:时序数据库 IoTDB 数据分区、同步与备份详解
  • 01_进程与线程
  • 第六届医学人工智能国际学术会议(ISAIMS 2025)