Git Product home page Git Product logo

Comments (3)

liuxinyu95 avatar liuxinyu95 commented on May 13, 2024

通常在C语言中,我们建议在宏上加括弧以避免副作用,尤其是运算符优先级引起的。但是就WORD_LENGTH来说,后继所有的使用都不存在这样的问题。例如:

#include <stdio.h>
#define WORD_LENGTH sizeof(int)*8

int main(int argc, char** argv) {
    printf("5 mod WL = %lu, 1<<(5 mod WL) = %d\n", 5 % WORD_LENGTH, 1 << (5 % WORD_LENGTH));
}
[xinyul@f45c89ba9a29:~/temp] $ gcc foo.c -Wall -o foo
[xinyul@f45c89ba9a29:~/temp] $ ./foo
5 mod WL = 8, 1<<(5 mod WL) = 256

from algoxy.

imhuay avatar imhuay commented on May 13, 2024

还是不对啊?

#define WL sizeof(int)*8
TEST()
{
    ASSERT_EQ(5 % WL, 5 % (sizeof(int) * 8));
}
Expected: 5 % sizeof(int)*8
      Which is: 8
To be equal to: 5 % (sizeof(int) * 8)
      Which is: 5

from algoxy.

liuxinyu95 avatar liuxinyu95 commented on May 13, 2024

非常感谢。%和*优先级相同,5 % sizeof(int) * 8 会先计算%,造成错误。
已修正

#define WORD_LENGTH (sizeof(int) * 8)

from algoxy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.