unsigned char i = -20

[flydream@flydream ThinkingInC++]$ cat unsigned.c
#include <stdio.h>
int main(int argc,char *argv[])
{
    unsigned char i;
    i = -20;
    printf("i = %d\n", i);
    return 0;
}

[flydream@flydream ThinkingInC++]$ gcc unsigned.c
[flydream@flydream ThinkingInC++]$ ./a.out
i = 236
[flydream@flydream ThinkingInC++]$ cat unsigned.cpp
#include <iostream>
using namespace std;

int main(int argc,char *argv[])
{    
    unsigned char i;
    i = -20;
    cout << "i = " << (int)i << endl;
    return 0;
    
}
[flydream@flydream ThinkingInC++]$ g++ unsigned.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
i = 236
[flydream@flydream ThinkingInC++]$


In an unsigned type, all the bits represent the value.If a type is defined for a particular machine to use 8 bits, then theunsigned version of this type could hold the values 0 through 255.

无符号型中,所有的位都表示数值。如果在某种机器中,定义一种类型使用 8 位表示,那么这种类型的unsigned 型可以取值 0 到 255。

The C++ standard does not define how signed types arerepresented at the bit level. Instead, each compiler is free to decide how itwill represent signed types. These representations can affect the rangeof values that a signed type can hold. We are guaranteed that an 8-bitsigned type will hold at least the values from 127 through 127; manyimplementations allow values from 128 through 127.

C++ 标准并未定义 signed 类型如何用位来表示,而是由每个编译器自由决定如何表示signed 类型。这些表示方式会影响 signed 类型的取值范围。8 位 signed类型的取值肯定至少是从 -127 到 127,但也有许多实现允许取值从 -128 到 127。

Under the most common strategy for representing signedintegral types, we can view one of the bits as a sign bit. Whenever the sign bitis 1, the value is negative; when it is 0, the value is either 0 or a positivenumber. An 8-bit integral signed type represented using a sign-bit canhold values from 128 through 127.

表示 signed 整型类型最常见的策略是用其中一个位作为符号位。符号位为 1,值就为负数;符号位为0,值就为 0 或正数。一个 signed 整型取值是从 -128 到 127。

Assignment to Integral Types
整型的赋值

The type of an object determines the values that the object canhold. This fact raises the question of what happens when one tries to assign avalue outside the allowable range to an object of a given type. The answerdepends on whether the type is signed or unsigned.

对象的类型决定对象的取值。这会引起一个疑问:当我们试着把一个超出其取值范围的值赋给一个指定类型的对象时,结果会怎样呢?答案取决于这种类型是signed 还是 unsigned 的。

For unsigned types, the compiler must adjust the out-of-range value so that it will fit.The compiler does so by taking the remainder of the value modulo the number ofdistinct values the unsigned target type can hold. An object that is an8-bit unsigned char, for example, can hold values from 0 through 255inclusive. If we assign a value outside this range, the compiler actuallyassigns the remainder of the value modulo 256. For example, we might attempt toassign the value 336 to an 8-bit signed char. If we try to store 336 inour 8-bit unsigned char, the actual value assigned will be 80, because80 is equal to 336 modulo 256.

对于 unsigned 类型来说,编译器必须调整越界值使其满足要求。编译器会将该值对unsigned 类型的可能取值数目求模,然后取所得值。比如 8 位的 unsigned char,其取值范围从 0 到255(包括 255)。如果赋给超出这个范围的值,那么编译器将会取该值对 256 求模后的值。例如,如果试图将 336 存储到 8 位的unsigned char 中,则实际赋值为 80,因为 80 是 336 对 256 求模后的值。

For the unsigned types, a negative value is always outof range. An object of unsigned type may never hold a negative value.Some languages make it illegal to assign a negative value to anunsigned type, but C++ does not.

对于 unsigned 类型来说,负数总是超出其取值范围。unsigned类型的对象可能永远不会保存负数。有些语言中将负数赋给 unsigned 类型是非法的,但在 C++ 中这是合法的。

 

In C++ it is perfectly legal to assign a negative number to an object with unsigned type. The result is the negative value modulo the size of the type. So, if we assign 1 to an 8-bit unsigned char, the resulting value will be 255, which is 1 modulo 256.

C++ 中,把负值赋给 unsigned 对象是完全合法的,其结果是该负数对该类型的取值个数求模后的值。所以,如果把 -1 赋给8位的 unsigned char,那么结果是 255,因为 255 是 -1 对 256 求模后的值。



When assigning an out-of-range value to a signed type,it is up to the compiler to decide what value to assign. In practice, manycompilers treat signed types similarly to how they are required totreat unsigned types. That is, they do the assignment as the remaindermodulo the size of the type. However, we are not guaranteed that the compilerwill do so for the signed types.

当将超过取值范围的值赋给 signed 类型时,由编译器决定实际赋的值。在实际操作中,很多的编译器处理signed 类型的方式和 unsigned类型类似。也就是说,赋值时是取该值对该类型取值数目求模后的值。然而我们不能保证编译器都会这样处理 signed 类型。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void I2C_3(unsigned char mcmd) { unsigned char length = 8; // Send Command while(length--) { if(mcmd & 0x80) { SDA3_1; } else { SDA3_0; } // uDelay(3); SCL3_1; // uDelay(3); SCL3_0; // uDelay(3); mcmd = mcmd << 1; } } void I2C_Ack3() { SDA3_1; // uDelay(3); SCL3_1; // uDelay(3); SCL3_0; // uDelay(3); } void I2C_NAck3() { SDA3_0; // uDelay(3); SCL3_1; // uDelay(3); SCL3_0; // uDelay(3); } void I2C_Start3() { SDA3_0; // uDelay(3); SCL3_1; // uDelay(3); SCL3_0; // uDelay(3); I2C_3(0x78); I2C_Ack3(); } void I2C_Stop3() { SCL3_1; // uDelay(5); SDA3_0; // uDelay(5); SDA3_1; // uDelay(5); } void Write_Command3(unsigned char Data) { I2C_Start3(); I2C_3(0x00); I2C_Ack3(); I2C_3(Data); I2C_Ack3(); I2C_Stop3(); } void Write_Data3(unsigned char Data) { I2C_Start3(); I2C_3(0x40); I2C_Ack3(); I2C_3(Data); I2C_Ack3(); I2C_Stop3(); } //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= void pic13(void) { unsigned char i,j; unsigned int k; k=0; for(j=0;j<8;j++) { Write_Command3(0x22);//--set page1 Write_Command3(j);//--set start page Write_Command3(0x07);//--set end page for(i=0;i<128;i++) //ÏÔʾµ¥É«Êý¾Ýµ½LCD { Write_Data3(color13[k]); k=k+1; } } } void pic14(void) { Uchar i,j; Uint k; k=0; for(j=0;j<8;j++) { Write_Command3(0x22);//--set page1 Write_Command3(j);//--set start page Write_Command3(0x07);//--set end page for(i=0;i<128;i++) //ÏÔʾµ¥É«Êý¾Ýµ½LCD { Write_Data3(color14[k]); k=k+1; } } } void LCD_Init3(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); delay_ms(500);这些代码分别是什么意思
最新发布
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值