Jump to content

Leading zero: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Line 15: Line 15:
== 0 as a prefix ==
== 0 as a prefix ==
{{seealso|Octal#In computers}}
{{seealso|Octal#In computers}}
A prefix <code>0</code> is used in the [[C (programming language)|C]], [[Python (programming language)|Python]], [[Perl (programming language)|Perl]], [[Ruby (programming language)|Ruby]], the [[Unix shell]] [[Bash (Unix shell)|bash]], and other computer programming languages to specify [[octal]] numbers (and <code>0x</code> means [[hexadecimal]]). As an example, <code>0020</code> does not represent 20<sub>10</sub> ( '''2'''*10<sup>1</sup> + '''0'''*10<sup>0</sup> ), but rather 20<sub>8</sub>=16<sub>10</sub> ( '''2'''*8<sup>1</sup> + '''0'''*8<sup>0</sup> = '''1'''*10<sup>1</sup> +'''6'''*10<sup>0</sup>). Decimal numbers written with leading zeros will likely be interpreted as octal, and will generate errors—not just unexpected results—if they contain '8' or '9' since these digits do not exist in octal. This behavior can be quite a nuisance when writing scripts to work with sequences of strings which have embedded zero-padded [[decimal]] numbers (typically file names) to facilitate ASCII sorting (see above).
A prefix <code>0</code> is used in the [[C (programming language)|C]], [[Python (programming language)|Python]], [[Perl (programming language)|Perl]], [[Ruby (programming language)|Ruby]], the [[Unix shell]] [[Bash (Unix shell)|bash]], and other computer programming languages to specify [[octal]] numbers (and <code>0x</code> means [[hexadecimal]]). As an example, <code>0020</code> does not represent 20<sub>10</sub> ( '''2'''*10<sup>1</sup> + '''0'''*10<sup>0</sup> ), but rather 20<sub>8</sub>=16<sub>10</sub> ( '''2'''*8<sup>1</sup> + '''0'''*8<sup>0</sup> = '''1'''*10<sup>1</sup> +'''6'''*10<sup>0</sup>). Decimal numbers written with leading zeros will be interpreted as octal in languages which follow this convention and will generate errors — not just unexpected results — if they contain '8' or '9' since these digits do not exist in octal. This behavior can be quite a nuisance when writing scripts to work with sequences of strings which have embedded zero-padded [[decimal]] numbers (typically file names) to facilitate ASCII sorting (see above).


The [[count leading zeros]] operation efficiently determines the number of leading zero bits in a machine word.
The [[count leading zeros]] operation efficiently determines the number of leading zero bits in a machine word.

Revision as of 21:53, 4 March 2013

A leading zero is any 0 digit that leads a number string in positional notation. For example, James Bond's famous identifier, 007, has two leading zeros. Leading zeros occupy most significant digits, which could be left blank or omitted for the same numeric value. Usual decimal notation of integers does not use leading zeros except for the zero itself, which would be denoted as an empty string otherwise.

Occurrence

Often, leading zeros are found on non-electronic digital displays or on such electronic ones as seven-segment displays, that contain fixed sets of digits. These devices include manual counters, stopwatches, odometers, and digital clocks. Leading zeros are also generated by many older computer programs when creating values to assign to new records, accounts and other files, and as such are likely to be used by utility billing systems, human resources information systems and government databases. Many digital cameras and other electronic media recording devices use leading zeros when creating and saving new files to make names of the equal length.

A leading zero appears in roulette in the United States, where '00' is distinct from '0' (a wager on '0' will not win if the ball lands in '00', and vice versa). Sports where competitors are numbered follow this as well; a stock car numbered '07' would be considered distinct from one numbered '7'. This is most common with single-digit numbers.

Advantages

Leading zeros are used to make ascending order of numbers correspond with alphabetical order: e.g., 11 comes alphabetically before 2, but after 02. (See, e.g., ISO 8601.) This does not work with negative numbers, though, whether leading zeroes are used or not: −23 comes alphabetically after −01, −1, and −22, although it is less than all of them.

Leading zeros can also be used to prevent fraud by filling in character positions that might normally be empty.

0 as a prefix

A prefix 0 is used in the C, Python, Perl, Ruby, the Unix shell bash, and other computer programming languages to specify octal numbers (and 0x means hexadecimal). As an example, 0020 does not represent 2010 ( 2*101 + 0*100 ), but rather 208=1610 ( 2*81 + 0*80 = 1*101 +6*100). Decimal numbers written with leading zeros will be interpreted as octal in languages which follow this convention and will generate errors — not just unexpected results — if they contain '8' or '9' since these digits do not exist in octal. This behavior can be quite a nuisance when writing scripts to work with sequences of strings which have embedded zero-padded decimal numbers (typically file names) to facilitate ASCII sorting (see above).

The count leading zeros operation efficiently determines the number of leading zero bits in a machine word.

See also