Nice programing

왜 16 진수를 사용합니까?

nicepro 2020. 12. 13. 11:06
반응형

왜 16 진수를 사용합니까?


야! http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html 에서이 코드를보고있었습니다 .

어떤 상황에서는 134 행과 같이 16 진수를 사용했음을 알았습니다.

for (j = 1; val && j <= 0x80; j <<= 1, q++)

이제 왜 0x80을 사용합니까? 나는 16 진수에 그다지 좋지 않지만 온라인 16 진수를 십진수로 찾았고 0x80에 대해 128을주었습니다.

또한 134 행 앞에 114 행에 다음이 있습니다.

small_n = (n & 0xffff0000) == 0;

16 진수를 10 진수로 표시하면 해당 16 진수에 4294901760이 표시됩니다. 그래서 여기이 줄에서 그들은 약간 AND를 만들고 그 결과를 0 ??

왜 그냥 번호를 사용하지 않습니까? 누구든지 설명하고 다른 상황의 예를 들어주세요.

또한 16 진수로만 이루어진 코드 줄을 보았고 그 이유를 결코 이해하지 못했습니다.


두 경우 모두 실제 숫자가 아니라 숫자의 비트 패턴이 중요합니다.

예를 들어, 첫 번째 경우 j에는 루프가 진행됨에 따라 1, 2, 4, 8, 16, 32, 64, 마지막으로 128이됩니다.

바이너리에서 즉,

0000:0001, 0000:0010, 0000:0100, 0000:1000, 0001:0000, 0010:0000, 0100:00001000:0000.

이 C 또는 C ++ 바이너리 상수에 대한 옵션이 없습니다하지만 진수에 조금 더 명확한이다 : 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40,와 0x80.

두 번째 예에서 목표는 값의 하위 2 바이트를 제거하는 것이 었습니다. 따라서 1,234,567,890의 값이 주어지면 1,234,567,168이됩니다.
16 진수에서는 더 명확합니다. 시작은 0x4996:02d2, 끝은 0x4996:0000.


16 진수 (또는 해당 문제에 대한 8 진수) 숫자와 기본 비트 패턴 사이에는 직접 매핑이 있습니다. 이는 10 진수의 경우가 아닙니다. 십진수 '9'는 어떤 열에 있고 그 주변의 숫자에 따라 비트 패턴과 관련하여 다른 것을 나타냅니다. 비트 패턴과 직접적인 관계가 없습니다. 16 진수에서 '9'는 어떤 열에 관계없이 항상 '1001'을 의미합니다. 9 = '1001', 95 = '* 1001 * 0101'등.

내 8 비트 시절의 흔적으로 16 진수가 바이너리에 대한 편리한 속기라는 것을 알았습니다. 비트 트위들 링은 죽어가는 기술입니다. 한 번 (약 10 년 전) 대학에서 3 년차 네트워킹 논문을 보았는데, 반원의 10 % (50 명 중 5 명)만이 비트 마스크를 계산할 수있었습니다.


약간의 마스크입니다. 16 진수 값을 사용하면 기본 이진 표현을 쉽게 볼 수 있습니다. n & n 0xffff0000의 상위 16 비트를 반환합니다. 0xffff0000"16 1s 및 16 0s in binary"를 의미합니다.

0x80 "1000000"을 의미하므로 "00000001"으로 시작하고 "1000000"까지 해당 비트를 왼쪽 "0000010", "0000100"등으로 계속 이동합니다.


0xffff0000은 32 비트 값에서 "1"의 16 배, "0"의 16 배, 4294901760은 마술이라는 것을 이해하기 쉽습니다.


나는 C 언어 계열이 항상 8 진법과 16 진법을 지원하지만 2 진법은 지원하지 않는다는 사실에 놀랐다. 나는 그들이 바이너리에 대한 직접적인 지원을 추가하기를 오랫동안 바랬습니다.

int mask = 0b00001111;

수년 / 일 전에 막대한 양의 비트 수준 수학이 포함 된 프로젝트에서 작업하는 동안 나는 질려서 최대 8 비트까지 가능한 모든 이진 값에 대해 정의 된 상수를 포함하는 헤더 파일을 생성했습니다.

#define b0        (0x00)
#define b1        (0x01)
#define b00       (0x00)
#define b01       (0x01)
#define b10       (0x02)
#define b11       (0x03)
#define b000      (0x00)
#define b001      (0x01)
...
#define b11111110 (0xFE)
#define b11111111 (0xFF)

때때로 특정 비트 수준 코드를 더 읽기 쉽게 만들었습니다.


16 진수의 가장 큰 용도는 아마도 임베디드 프로그래밍에 있습니다. 16 진수는 하드웨어 레지스터의 개별 비트를 마스킹하거나 단일 8, 16 또는 32 비트 레지스터로 압축 된 여러 숫자 값을 분할하는 데 사용됩니다.

개별 비트 마스크를 지정할 때 많은 사람들이 다음과 같이 시작합니다.

#define bit_0 1
#define bit_1 2
#define bit_2 4
#define bit_3 8
#define bit_4 16
etc...

잠시 후 다음으로 진행됩니다.

#define bit_0 0x01
#define bit_1 0x02
#define bit_2 0x04
#define bit_3 0x08
#define bit_4 0x10
etc...

그런 다음 속임수를 배우고 컴파일러가 컴파일 시간 최적화의 일부로 값을 생성하도록합니다.

#define bit_0 (1<<0)
#define bit_1 (1<<1)
#define bit_2 (1<<2)
#define bit_3 (1<<3)
#define bit_4 (1<<4)
etc...

Sometimes the visual representation of values in HEX makes code more readable or understandable. For instance bitmasking or use of bits becomes non-obvious when looking at decimal representations of numbers.

This can sometimes do with the amount of space a particular value type has to offer, so that may also play a role.

A typical example might be in a binary setting, so instead of using decimal to show some values, we use binary.

let's say an object had a non-exclusive set of properties that had values of either on or off (3 of them) - one way to represent the state of those properties is with 3 bits.

valid representations are 0 through 7 in decimal, but that is not so obvious. more obvious is the binary representation:

000, 001, 010, 011, 100, 101, 110, 111

Also, some people are just very comfortable with hex. Note also that hard-coded magic numbers are just that and it is not all that important no matter numbering system to use

I hope that helps.


Generally the use of Hex numbers instead of Decimal it's because the computer works with bits (binary numbers) and when you're working with bits also is more understandable to use Hexadecimal numbers, because is easier going from Hex to binary that from Decimal to binary.

OxFF = 1111 1111 ( F = 1111 )

but

255 = 1111 1111 

because

255 / 2 = 127 (rest 1)
127 / 2 = 63 (rest 1)
63 / 2 = 31 (rest 1)
... etc

Can you see that? It's much more simple to pass from Hex to binary.


There are 8 bits in a byte. Hex, base 16, is terse. Any possible byte value is expressed using two characters from the collection 0..9, plus a,b,c,d,e,f.

Base 256 would be more terse. Every possible byte could have its own single character, but most human languages don't use 256 characters, so Hex is the winner.

To understand the importance of being terse, consider that back in the 1970's, when you wanted to examine your megabyte of memory, it was printed out in hex. The printout would use several thousand pages of big paper. Octal would have wasted even more trees.


Hex, or hexadecimal, numbers represent 4 bits of data, 0 to 15 or in HEX 0 to F. Two hex values represent a byte.


To be more precise, hex and decimal, are all NUMBERS. The radix (base 10, 16, etc) are ways to present those numbers in a manner that is either clearer, or more convenient.

When discussing "how many of something there are" we normally use decimal. When we are looking at addresses or bit patterns on computers, hex is usually preferred, because often the meaning of individual bytes might be important.

Hex, (and octal) have the property that they are powers of two, so they map groupings of bit nicely. Hex maps 4 bits to one hex nibble (0-F), so a byte is stored in two nibbles (00-FF). Octal was popular on Digital Equipment (DEC) and other older machines, but one octal digit maps to three bits, so it doesn't cross byte boundaries as nicely.

Overall, the choice of radix is a way to make your programming easier - use the one that matches the domain best.


Looking at the file, that's some pretty groady code. Hope you are good at C and not using it as a tutorial...

Hex is useful when you're directly working at the bit level or just above it. E.g, working on a driver where you're looking directly at the bits coming in from a device and twiddling the results so that someone else can read a coherent result. It's a compact fairly easy-to-read representation of binary.

참고URL : https://stackoverflow.com/questions/243712/why-use-hex

반응형