Giter Club home page Giter Club logo

notes's Issues

位运算

#include <stdio.h>

#define PRINT(x) printf("hex: 0x%08x, dec: %12d, str: \"%s\"\n", x, x, #x)

int main()
{
    puts( "全 0 & 全 1" );
    PRINT( 0 );
    PRINT( -1 );

    puts( "\n全 1 的左右 shift" );
    PRINT( -1>>1 );
    PRINT( -1<<1 );

    puts( "\n减法优先级更高" );
    PRINT( 1<<31-1 );
    PRINT( 1<<30 );

    puts( "\n怎么得到 int 的最大正数" );
    PRINT( 1<<31 );
    PRINT( (1<<31)-1 );
    PRINT( ((unsigned int)1<<31)-1 );
    PRINT( ((unsigned int)-1)>>1 );
    PRINT( -1>>1 );
    PRINT( 0x7FFFFFFF );
}

编译:gcc test.c -o test

运行:./test

结果:

全 0 & 全 1
hex: 0x00000000, dec:            0, str: "0"
hex: 0xffffffff, dec:           -1, str: "-1"

全 1 的左右 shift
hex: 0xffffffff, dec:           -1, str: "-1>>1"
hex: 0xfffffffe, dec:           -2, str: "-1<<1"

减法优先级更高
hex: 0x40000000, dec:   1073741824, str: "1<<31-1"
hex: 0x40000000, dec:   1073741824, str: "1<<30"

怎么得到 int 的最大正数
hex: 0x80000000, dec:  -2147483648, str: "1<<31"
hex: 0x7fffffff, dec:   2147483647, str: "(1<<31)-1"
hex: 0x7fffffff, dec:   2147483647, str: "((unsigned int)1<<31)-1"
hex: 0x7fffffff, dec:   2147483647, str: "((unsigned int)-1)>>1"
hex: 0xffffffff, dec:           -1, str: "-1>>1"
hex: 0x7fffffff, dec:   2147483647, str: "0x7FFFFFFF"

不要被别人的 memset(buf, -1, sizeof(buf)) 迷惑了

有文件 test.c

#include <stdio.h>
#include <string.h>

int main()
{
    int m[2];
    printf( "sizeof(m): %lu\n", sizeof(m) );

    memset( m, -1, sizeof(m) );
    printf( "after memset(m,   -1, sizeof(m)): %20d (0x%08x), %20d (0x%08x)\n", m[0], m[0], m[1], m[1] );

    memset( m,  1, sizeof(m) );
    printf( "after memset(m,    1, sizeof(m)): %20d (0x%08x), %20d (0x%08x)\n", m[0], m[0], m[1], m[1] );

    memset( m, 0xFF, sizeof(m) );
    printf( "after memset(m, 0xFF, sizeof(m)): %20d (0x%08x), %20d (0x%08x)\n", m[0], m[0], m[1], m[1] );

    memset( m, 0xA5, sizeof(m) );
    printf( "after memset(m, 0xA5, sizeof(m)): %20d (0x%08x), %20d (0x%08x)\n", m[0], m[0], m[1], m[1] );
}

编译运行

$ gcc test.c -o test
$ ./test
sizeof(m): 8
after memset(m,   -1, sizeof(m)):                   -1 (0xffffffff),                   -1 (0xffffffff)
after memset(m,    1, sizeof(m)):             16843009 (0x01010101),             16843009 (0x01010101)
after memset(m, 0xFF, sizeof(m)):                   -1 (0xffffffff),                   -1 (0xffffffff)
after memset(m, 0xA5, sizeof(m)):          -1515870811 (0xa5a5a5a5),          -1515870811 (0xa5a5a5a5)

所以,要小心了。别人用 memset(buf, -1, sizeof(buf)) 来初始化 buf,你用 -2 可不是 -2 啊!

这涉及到 two's compliment 编码。


另一个不符合直觉的是 int buf[50] = { 0 }; 确实会把所有元素初始化为 0,但是 int buf[50] = { 3 }; 只回初始化第一个元素为 3,其它都是 0……至少在我的 gccg++ 上都是如此。

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.