157

What does a 0x prefix on a number mean?

const int shared_segment_size = 0x6400;

It's from a C program. I can't recall what it amounts to and particularly what the letter x means.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Terry Li
  • 16,870
  • 30
  • 89
  • 134

5 Answers5

159

Literals that start with 0x are hexadecimal integers. (base 16)

The number 0x6400 is 25600.

6 * 16^3 + 4 * 16^2 = 25600

For an example including letters (also used in hexadecimal notation where A = 10, B = 11 ... F = 15)

The number 0x6BF0 is 27632.

6 * 16^3 + 11 * 16^2 + 15 * 16^1 = 27632
24576    + 2816      + 240       = 27632
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 4
    What I was wondering about, how should negative numbers be denoted in hex? Like -0xff or 0x-ff? I guess the first, but I don't know. – Luc Jul 23 '13 at 15:13
  • 10
    @Luc The first one is correct. Although more often I see just `0xffffffff` and such. (`0xffffffff = -1` for 32-bit int assuming 2's complement) – Mysticial Jul 23 '13 at 16:30
  • 1
    @Luc Negatives numbers are represented in binary using *2's complement*- a clever way that does not require '-' sign. So, it's basically converting from binary to hex, negative or positive. – Shuvo Sarker Mar 06 '20 at 21:05
  • 1
    @ShuvoSarker Thanks for the addition! That's just the memory representation though, I guess similar to if we would agree that we have a set of numbers from 0--999 and that 1000--1999 would represent the negative numbers. My question was about human-readable notation, though knowing how computers do it might help someone else, so thanks :) – Luc Mar 07 '20 at 10:07
  • You need to fix this answer. The first sentence is misleading in the extreme. Literals that start with `0x` can also obviously be IEEE 754 floating point values, as in hexadecimal exponential notation. Your answer would suggest otherwise and currently deserves a downvote. – Excel Hero May 30 '22 at 14:43
24

In C and languages based on the C syntax, the prefix 0x means hexadecimal (base 16).

Thus, 0x400 = 4×(162) + 0×(161) + 0×(160) = 4×((24)2) = 22 × 28 = 210 = 1024, or one binary K.

And so 0x6400 = 0x4000 + 0x2400 = 0x19×0x400 = 25K

halfer
  • 19,824
  • 17
  • 99
  • 186
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
10

It's a hexadecimal number.

0x6400 translates to 4*16^2 + 6*16^3 = 25600

Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
knittl
  • 246,190
  • 53
  • 318
  • 364
8

SIMPLE

It's a prefix to indicate the number is in hexadecimal rather than in some other base. The C programming language uses it to tell compiler.

Example :

0x6400 translates to 6*16^3 + 4*16^2 + 0*16^1 +0*16^0 = 25600. When compiler reads 0x6400, It understands the number is hexadecimal with the help of 0x term. Usually we can understand by (6400)16 or (6400)8 or any base ..

Hope Helped in some way.

Good day,

loyola
  • 3,905
  • 2
  • 24
  • 18
8

The numbers starting with 0x are hexadecimal (base 16).0x6400 represents 25600.

To convert,

  • multiply the last digit times 1
  • add second-last digit times 16 (16^1)
  • add third-last digit times 256 (16^2)
  • add fourth-last digit times 4096 (16^3)
  • ...and so on

The factors 1, 16, 256, etc. are the increasing powers of 16.

0x6400 = (0*1) + (0*16^1) + (4*16^2) + (6*16^3) = 25600 

or

0x6400 = (0*1) + (0*16) + (4*256) + (6*4096) = 25600 
Sithu
  • 4,752
  • 9
  • 64
  • 110