可以将十六进制数字与十进制数字相加/相减吗? [英] Can hexadecimal numbers be added/subtracted with decimal numbers?

查看:264
本文介绍了可以将十六进制数字与十进制数字相加/相减吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中编程时,请说我是否将整数h作为十六进制值,并将d作为十进制数.我可以在h和d之间做加法或减法吗?还是必须在同一号码系统中?

When programming in C, say if I have integer h as a hexadecimal value and integer d as a decimal number. Can I do addition or subtraction between h and d? Or do they have have to be in the same number system?

推荐答案

是的,您可以编写:

int x = 100 - 0x100 + 0100;

将十进制与十六进制和八进制混合.无论如何,在计算发生之前,所有值都将转换为二进制值(在此示例中,编译器将进行计算;在运行时不会对其进行评估).并且任何常量都可以替换为分配了适当值的int值:

That mixes decimal with hex and octal. The values are all converted to binary anyway before the calculation occurs (and the compiler will do the calculation in this example; it won't be evaluated at runtime). And any of the constants can be replaced by an int value that was assigned the appropriate value:

int d = 100;
int h = 0x100;
int o = 0100;
int x = d + h + o;

这篇关于可以将十六进制数字与十进制数字相加/相减吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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