需要帮助理解十六进制输入 [英] Need help understanding hexadecimal inputs

查看:99
本文介绍了需要帮助理解十六进制输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此代码

Given this code

#include <stdio.h>
 
int main(void)
{
  int a[3] = {0x80000000 , 0x80000000 , 0x80000000};
  int i , j , k;
  for ( i = 0 ; i < 10 ; ++i )
  { 
int tmp;
scanf("%d",&tmp);
for(j=0 ; j<3;++j)
{ 
if(tmp>a[j])
{ int k;
for(k=2;k>j;--k)
{ 
a[k]=a[k-1];
}
a[j] = tmp;
break;
}
}
  }
  printf("%d %d %d\n",a[0] , a[1] , a[2]);
  return 0;
}





我的尝试:



这将在用户输入的数组中输出3个最大数字。

我对数组如何工作有一个大概的了解,但我没有得到的是这一行。





What I have tried:

This will output 3 largest number in the array that the user has input.
I have a rough idea of how an array works but what I don't get is this line.

int a[3] = {0x80000000 , 0x80000000 , 0x80000000};





如何将[3]初始化为0x8000000?



how does initializing a[3] to 0x8000000 works?

推荐答案

int a[3] = {0x80000000 , 0x80000000 , 0x80000000};



它创建一个包含三个元素的数组,所有元素都包含相同的值:0x80000000

由于整数值是32位,它可以容纳的最小十六进制值是0x00000000,最大的0xFFFFFFFF - 但是有一个皱纹。

整数是有符号值:它们不会从0运行到4,294,967,295(严格解释为0x0到0xFFFFFFFF),而是存储正负值,给出一系列 - 2,147,483,648到2,147,483,647。

他们通过说任何具有最高位设置的数字是负数,否则它是正数

最重要的是bi t 31,最不重要的是位数0.

如果你设置位数31并重置所有其他,你得到:


It creates an array with three elements, all of which contain the same value: 0x80000000
Since an integer value is 32 bits, the smallest hexadecimal value it can hold is 0x00000000, and the largest 0xFFFFFFFF - but there is a wrinkle.
integers are signed values: they don't run from 0 to 4,294,967,295 (the strict interpretation of 0x0 to 0xFFFFFFFF) instead they store positive and negative values, giving a range of -2,147,483,648 to 2,147,483,647.
And they do this by saying "any number with the most significant bit set is negative, otherwise it's positive"
The most significant bit is bit number 31, the least significant is bit number 0.
And if you set bit number 31 and reset all the others, you get:

10000000000000000000000000000000 in binary
80000000 in hexadecimal

这是一个负数,实际上是你可以装入32位有符号数的最小值:-2,147,483,648



所以你有一个包含三个元素的数组,每个元素都包含系统给你的最小值。

Which is a negative number, and is actually the minimum value you can fit in a 32 bit signed number: -2,147,483,648

So you have an array with three elements, each of which contains the minimum value the system can give you.


这一行在左边是声明在右边,数组的初始化。它是常见的C ++代码,但需要写成一行。重要的是要知道,括号中列表的长度必须适合数组大小。



查看一些关于数组初始值设定项
This line is on the left the declaration and on the right the initialization of the array. It is common C++ code but needs to be written as one line. Important is to know, that length of the list in the brackets must fit to the array size.

See some article about array initializer.



$ b $相同b
It is the same as
int a[3] = {-2147483648, -2147483648 , -2147483648};

0x 前缀指定以下数字被解释为十六进制数字。有关阵列初始化的信息,请参阅阵列初始化 - cppreference.com [ ^ ]。



你也可以使用

The 0x prefix specifies that the following digits are interpreted as hex digits. For array initialisation see Array initialization - cppreference.com[^].

You might also use

#include <limits.h>

int a[3] = {INT_MIN, INT_MIN, INT_MIN};


这篇关于需要帮助理解十六进制输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆