해킹 을 읽는 동안 : 착취 (! 멋진 책)의 예술을, 나는이 기능을 통해 온 작성자 :이 기능의 작동 방식은 무엇입니까?
= = = = = = = = = = = = = = = = = = = = = =
INPUT : OUTPUT
= = = = = = = = = = = = = = = = = = = = = =
0 : 00000000 00000000 00000000 00000000
2 : 00000000 00000000 00000000 00000010
1 : 00000000 00000000 00000000 00000001
1024 : 00000000 00000000 00000100 00000000
512 : 00000000 00000000 00000010 00000000
64 : 00000000 00000000 00000000 01000000
= = = = = = = = = = = = = = = = = = = = = =
: 여기
void binary_print(unsigned int value) {
unsigned int mask = 0xff000000; // Start with a mask for the highest byte.
unsigned int shift = 256*256*256; // Start with a shift for the highest byte.
unsigned int byte, byte_iterator, bit_iterator;
for(byte_iterator=0; byte_iterator < 4; byte_iterator++) {
byte = (value & mask)/shift; // Isolate each byte.
printf(" ");
for(bit_iterator=0; bit_iterator < 8; bit_iterator++) { // Print the byte's bits.
if(byte & 0x80) // If the highest bit in the byte isn't 0,
printf("1"); // print a 1.
else
printf("0"); // Otherwise, print a 0.
byte *= 2; // Move all the bits to the left by 1.
}
mask /= 256; // Move the bits in mask right by 8.
shift /= 256; // Move the bits in shift right by 8.
}
}
는 함수에 대한 입력 - 출력 테이블의 이것은 binary_print()가 10 진수를 2 진수로 변환한다는 것을 알고 있습니다.
하지만 이해가 안됩니다 정확히 함수가 정답을 찾습니다. 구체적으로 :
- 마스크 란 무엇입니까? 작성자는 0xff000000 값에 어떻게 도달 했습니까? (0xff000000은 시스템의 int 값의 최대 값 인 2^32에 가까워졌습니다.)
- 교대 란 무엇입니까? 256^3으로 초기화하는 이유는 무엇입니까? 는 실제로이 라인에 어떻게됩니까
- (이것은 16 진수로 장소 무게와 함께 할 수있는 뭔가가 같은 나에게 보인다) :
- 바이트 = (값 & 마스크)/이동
- 바이트 & 0x80으로
즉, 나는 변환을 수행하는 데 사용) (에게 방법 binary_print을 이해하고 싶습니다.
C 세계에도 @ e-satis가 있는가? (http://stackoverflow.com/users/9951/e-satis).예를 들어,이 질문에는 python yield 키워드에 대한 대답과 같은 대답이 필요합니다. http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained/231855#231855 – Yavar
MSDN : [C 비트 연산자 ] (http://msdn.microsoft.com/en-us/library/17zwb64t.aspx) 및 [비트 시프트 연산자] (http://msdn.microsoft.com/en-us/library/f96c63ed.aspx) Wikipedia : [바이너리 계산] (http://en.wikipedia.org/wiki/Binary_number#Counting_in_binary) :) – parrowdice