简体   繁体   English

具有相同逻辑的两段代码。 一个提供预期的输出,而另一个则没有

[英]Two pieces of code with the same logic. One gives expected output while the other does not

char xtime(char m)
{
    //calculates the m value by checking m,if m is less than 0x80 hexadecimal
    // then it is left shifted else it is left shifted and xor'ed with 0x1b.
    if(m<0x80)
    {
        m<<=1;
    } else {
        m=(((m)<<1)^0x1b);
    }

    printf("%#01x ",m&0xff);
    return m;
}

This code doesnt show the expected output if m=0x80(which is 0x1b),it gives the output as 0 in hexadecimal. 如果m = 0x80(即0x1b),则此代码未显示预期的输出,它将输出以十六进制表示为0。

#define xtime(a) (((a)<0x80)?(a)<<1:(((a)<<1)^0x1b) )

This code works and gives the expected result . 此代码有效,并给出了预期的结果。

Could you please help as to what is wrong with the functional code and how is it solved in the second code. 关于功能代码的问题以及如何在第二代码中解决,请您提供帮助。

Assuming that in your environment 假设在您的环境中

  • char is signed char已签名
  • char is 8-bit long char是8位长
  • two's compliment is used to express negative integers 二的补语用于表示负整数
  • if a signed integer to convert to cannot store original value, upper bits are simply thrown away 如果要转换为带符号的整数不能存储原始值,则仅丢弃高位

0x80 is too big to store into char variable, and it will be interpreted as -128 . 0x80太大,无法存储到char变量中,它将被解释为-128 -128 is smaller than 0x80 , so m <<= 1; -128小于0x80 ,因此m <<= 1; is executed. 被执行。 The result of this shift is -256 , and its binary reprensentation is 0xffffff00 , and m will get the last 8 bits, which is 0 . 此移位的结果是-256 ,其二进制表示形式是0xffffff00 ,并且m将获得最后8位,即0 That is what you get. 那就是你得到的。

The macro will work if 0x80 is passed to a , because the calculation will be done using int , and int can hold integers upto at least 32767 . 如果将0x80传递给a ,则该宏将起作用,因为该计算将使用int完成,并且int最多可容纳至少32767整数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RHEL 6.0两段相似的代码:一编译不 - RHEL 6.0 Two Similar Pieces of Code: One Compiles One Does Not 两个代码; 相同的逻辑和代码行,一个有效,但另一个无效 - Two codes; same logic and line of code, one works but other doesn't 开发 C++ 提供了一个 output,但 Visual Studio 代码为相同的代码提供了另一个 output - Dev C++ gives one output, but Visual studio code gives another output for same code 此代码后缀和前缀在一行中的同一变量的输出背后的逻辑是什么 C++ - What is logic behind output of this code postfix and prefix on same varible in one line c++ 这两段代码有什么区别吗? - Is there any difference these two pieces of code? CUDA:是否可以将一个内核视为“主”来执行 memory malloc,并运行其他“逻辑代码”? - CUDA: Does it possible to treate one core as “master” to do memory malloc, and run other “logic code”? 伪随机数发生器给出相同的第一输出,但随后表现如预期 - Pseudo random number generator gives same first output but then behaves as expected 为什么这个代码会创建两个对象而需要一个? - Why does this code create two objects while one is needed? 当相同的源代码在两个不同的编译器下给出不同的答案时,它意味着什么? - What does it mean when the same source code gives different answers under two different compilers? (C ++)比较两段代码,一个有效,一个无效,无法找出区别 - (C++) Comparing two pieces of code, one works and one doesn't, can't figure out the difference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM