当前位置:网站首页>二进制补码
二进制补码
2022-07-25 15:22:00 【一笑杯莫停】
刚刚遇到一个题,求b的值
byte b = (byte)129;
刚拿到题,我们一看,哎,这不是强制类型转换吗?byte是有符号的,于是乎我们就开始算了:
byte一个字节,也就是8位二进制那129就是:10000001
一看是有符号的,那就是符号位不变,其他位置反码+1:11111110+1=11111111=-127(1+2+4+8+16+32+64)
balabala~算了半天,半小时过去了,那么有没有更好的办法呢?
我们知道,byte是有符号单字节整形,所以存储数字范围是[-128·127],补码就应该上面那么算。但是为什么要这么算呢?
因为科学家定义数字的时候是一个环,最大的数字后面就是最小,这样才可以把[0·255]分配给[-128·127]。
底层就是补码的概念。
所以我们来数数,129大于byte表达最大的整数127.而且是一个环,那就是127过后就是byte最小的负数-128,-127 。好了就是-127了,就这么回事。是不是分分钟解决?
边栏推荐
- Rediscluster setup and capacity expansion
- Recommend 10 learning websites that can be called artifact
- Application of object detection based on OpenCV and yolov3
- Simulate setinterval timer with setTimeout
- 记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!
- spark中saveAsTextFile如何最终生成一个文件
- Scala110-combineByKey
- 任务、微任务、队列和调度(动画展示每一步调用)
- 解决DBeaver SQL Client 连接phoenix查询超时
- mysql heap表_MySQL内存表heap使用总结-九五小庞
猜你喜欢

Boosting之GBDT源码分析

How to solve the login problem after the 30 day experience period of visual stuido2019

基于OpenCV和YOLOv3的目标检测实例应用

npm的nexus私服 E401 E500错误处理记录

在win10系统下使用命令查看WiFi连接密码

Example of password strength verification

ML - natural language processing - Basics

Idea护眼色设置

记一次Spark foreachPartition导致OOM

ML - 语音 - 传统语音模型
随机推荐
Spark memory management mechanism new version
Is it safe to open futures online? Which company has the lowest handling charge?
异步fifo的实现
什么是物联网
JVM dynamic bytecode technology details
Spark submission parameters -- use of files
RedisCluster搭建和扩容
如何解决Visual Stuido2019 30天体验期过后的登陆问题
ios 面试题
数据系统分区设计 - 分区再平衡(rebalancing)
How spark gets columns in dataframe --column, $, column, apply
MySQL transactions and mvcc
Understanding the execution order of T-SQL query from the execution order of join on and where
Promise object and macro task, micro task
Instance tunnel use
Graph theory and concept
Spark002---spark任务提交,传入json作为参数
树莓派入门:树莓派的初始设置
反射-笔记
Spark sql 常用时间函数