문제

#include <iostream>
#include <stdint.h>
struct Foo 
{
    int a : 2;
    int b : 2;
    int c : 2;
    int d : 2;
};
int main()
{
    Foo foo;
    foo.d = 2;
    std::cout << sizeof(foo) << std::endl;
    std::cout << foo.d << std::endl;
    return 0;
}

Using g++, the result turns to be 4 -2, how could foo.d became -2?

도움이 되었습니까?

해결책 2

first one is sign bit. for 2 (10) first one is sign 1 so its negative number. 2's compliment of 10 you will get -2.

다른 팁

int is signed by default, so an int :2 has a range of -2 through 1. Use unsigned int :2 if you want to be able to store values 0 through 3 instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top