当前位置:网站首页>二进制补码
二进制补码
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了,就这么回事。是不是分分钟解决?
边栏推荐
- The number of query results of maxcompute SQL is limited to 1W
- 记一次Spark foreachPartition导致OOM
- ML - natural language processing - Introduction to natural language processing
- Use the command to check the WiFi connection password under win10 system
- vscode 插件篇收集
- Image cropper example
- UIDocumentInteractionController UIDocumentPickerViewController
- Scala110-combineByKey
- Sublimetext-win10 cursor following problem
- How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?
猜你喜欢

Graph theory and concept

spark分区算子partitionBy、coalesce、repartition

Understanding the execution order of T-SQL query from the execution order of join on and where

Image cropper example

ML - 自然语言处理 - 自然语言处理简介

How much memory can a program use at most?

Boosting之GBDT源码分析

Implementation of asynchronous FIFO

Remember that spark foreachpartition once led to oom

Introduction to raspberry Pie: initial settings of raspberry pie
随机推荐
JVM-参数配置详解
本地缓存--Ehcache
Object.prototype.hasOwnProperty() 和 in
我的创作纪念日
如何解决Visual Stuido2019 30天体验期过后的登陆问题
Scala110-combineByKey
RedisCluster搭建和扩容
pkg_ Resources dynamic loading plug-in
The development summary of the function of fast playback of audio and video in any format on the web page.
Xcode添加mobileprovision证书文件报错:Xcode encountered an error
记一次redis超时
记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!
Endnote 添加中文GBT7714样式 word中如何引用文献
Application of C language array in Sanzi chess -- prototype of Queen n problem
Hbck 修复问题
ios 面试题
Is it safe to open futures online? Which company has the lowest handling charge?
Overview of JS synchronous, asynchronous, macro task and micro task
Debounce and throttle
ML - 自然语言处理 - 基础知识