0
\$\begingroup\$

For the code, which I am referring following is syntax:

Write_Parameter(0x00);

In above line, what does "x" stands for and what is data size for x? My assumption is its dec format.

So, What other format, we can use?

Thank you.

\$\endgroup\$
  • 2
    \$\begingroup\$ 0x prefix means hexadecimal. \$\endgroup\$ – Null Jul 2 '15 at 5:07
  • \$\begingroup\$ Thanks. So, 1 bit of Hex represented by four binary bits? What are other formats used in coding format? \$\endgroup\$ – Electroholic Jul 2 '15 at 5:08
  • \$\begingroup\$ @Electroholic: "So, 1 bit of Hex represented by four binary bits? What are other formats used in coding format?" No! "1 bit of Hex" is not the term to use. See the answers below. \$\endgroup\$ – WedaPashi Jul 2 '15 at 5:41
  • \$\begingroup\$ One hex digit is represented as 4 bits since there's 16 digits - 0 through F. \$\endgroup\$ – Alexxx Jul 2 '15 at 18:54
9
\$\begingroup\$

0x followed by a series of digits means a hexadecimal number in C and many other languages (other common formats are decimal, octal, and binary).

The number of digits after the x represents the number of bits, in multiples of 4:

0x0        - 4 bits (or one "nibble")
0x00       - 8 bits (or one byte)
0x0000     - 16 bits
0x000000   - 24 bits
0x00000000 - 32 bits

But as Nick Johnson points out, regardless of the number of digits in the constant, in C a numeric constant is treated as an int unless it has an l or L suffix, or preceded by a cast. On 8 and 16-bit machines, an int is usually 16 bits, and on 32-bit machines it is 32-bits.

Because the number is hexadecimal, each digit can represent one of 16 values, 0-9 and A-F (A=10, B=11, C=12, D=13, E=14 and F=15).

Each digit position, going from right to left, represents a hexadecimal "nibble" or four bits, with a placeholder value of 1, 16, 256, 4096, 65536, etc.

So 0x0ABC, for example would equal decimal 2748:

4096   256   16     1

  0     A     B     C     =>     0*4096 + 10*256 * 11*16 + 12*1 = 2748

The 0 after the x could be omitted and you'd end up with the same thing.

The largest unsigned value in each of the fields above is:

0xf        - 15         (2⁴-1)
0xff       - 255        (2⁸-1)
0xffff     - 65535      (2¹⁶-1)
0xffffff   - 1677215    (2²⁴-1)
0xffffffff - 4294967295 (2³²-1)
\$\endgroup\$
  • \$\begingroup\$ :-) Removing the comments now, as they are obsolete once the post is edited. \$\endgroup\$ – WedaPashi Jul 2 '15 at 5:41
  • \$\begingroup\$ It's worth noting that in C, at least, the length of the hexadecimal numeric constant has no bearing on how large the value is: by default the type of a numeric constant is 'int' unless you add the 'l' or 'll' suffix. \$\endgroup\$ – Nick Johnson Jul 2 '15 at 8:43
  • \$\begingroup\$ @NickJohnson Thanks, updated my answer. I assume a cast would also modify the size of the constant. \$\endgroup\$ – tcrosley Jul 2 '15 at 18:43
5
\$\begingroup\$

If you are using C (you did not specify), data can be represented in the following ways:

  • Numbers starting with 0 are octal (eg 0123 is binary 001010011)
  • Numbers starting with 0x are hexadecimal (eg 0x1A is binary 00011010)
  • Numbers starting with 0b are binary
  • Numbers starting with digits 1-9 are decimal
  • 'a' is a character which represent the decimal value 97 (binary 01100001)

Some compilers (not many as this is not part of standard C) accept 0b as binary. Gcc does not (Thanks @Rev1.0 for the correction!).

\$\endgroup\$
  • 3
    \$\begingroup\$ GCC actually supports binary literal with 0b prefix. \$\endgroup\$ – Rev1.0 Jul 2 '15 at 5:52
  • \$\begingroup\$ 'a' has the value used to represent the character 'a' by the compiler. Character encodings are almost always ASCII these days, which means the value is decimal 97 on almost all systems, but that is not required by the language definition. If you need a decimal value 97, use 97. <g> \$\endgroup\$ – Pete Becker Jul 2 '15 at 17:28
  • \$\begingroup\$ Of course - if you need 97, it's better to use the decimal value. I'd rather not use 0b01100001 either. I just wanted to indicate that characters in C are in fact just 'short integers'. After all, you can do printf("%d\n", 'a' * 5);. It's not as if 'a' is considered another class, like in Python, Ruby. \$\endgroup\$ – jcoppens Jul 2 '15 at 17:53
3
\$\begingroup\$

In C (and many other programming languages) "0x" indicates a hexadecimal number.

Since your example shows two digits after the "0x", that parameter must be 8 bits. Each hex digit represents four bits.

"0b" would indicate binary. If no prefix, the number is decimal.

\$\endgroup\$
  • \$\begingroup\$ 0x00 is an int with the value 0. It is at least 16 bits wide. You're probably right that the function only cares about the low 8 bits, but that doesn't affect the value of the literal constant. \$\endgroup\$ – Pete Becker Jul 2 '15 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.