# Decode date and time in a sequence of binary data

In computing and electronic systems, binary-coded decimal BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bitsusually four or eight. Special bit patterns are sometimes used for a sign or for other indications e. In byte-oriented systems i. The precise 4-bit encoding may vary however, for technical reasons, see Excess-3 for instance.

BCD's main virtue is its more accurate representation and rounding of decimal quantities as well as an ease of conversion into human-readable representations, in comparison to binary positional systems. BCD's principal drawbacks are a small increase in the complexity of the circuits needed to implement basic arithmetics and a slightly less dense storage.

Although BCD per se is not as widely used as in the past and is no longer implemented in newer computers' instruction sets such as ARM ; x86 does not support BCD instructions in long mode any moredecimal fixed-point and floating-point formats are still important and continue to be used in financial, commercial, and industrial computing, where subtle conversion and fractional rounding errors that are inherent in floating point binary representations cannot be tolerated.

BCD takes advantage of the fact that any one decimal numeral can be represented by a four bit pattern. The most obvious way of encoding digits is "natural BCD" NBCDwhere each decimal digit is represented by its corresponding four-bit binary value, as shown in the following table. This is also called "" encoding.

Other encodings are also used, including so-called "" and ""—named after the weighting used for the bits—and " Excess-3 ". As most computers deal with data in 8-bit bytesit is possible to use one of the following methods to encode a BCD number:.

As an example, encoding the decimal number 91 using unpacked BCD results in the following binary pattern of two bytes:. Hence the numerical range for one unpacked BCD byte is zero through nine inclusive, whereas the range for one packed BCD is zero through ninety-nine inclusive. To represent numbers larger than the range of a single byte any number of contiguous bytes may be used.

For example, to represent the decimal number in packed BCD, using big-endian format, decode date and time in a sequence of binary data program would encode as follows:. Note that the most significant nibble of the most significant byte is zero, implying that the number is in actuality Also note how packed BCD is more efficient in storage usage as compared to unpacked BCD; encoding the same number with the leading zero in unpacked format would consume twice the storage.

Shifting and masking operations are used to pack or unpack a packed BCD digit. Other logical operations are used to convert a numeral to its equivalent bit pattern or reverse the process.

BCD is very common in electronic systems where a numeric value is to be displayed, especially in systems consisting solely of digital logic, and not containing a microprocessor. By employing BCD, the manipulation of numerical data for display can be greatly simplified by treating each digit as a separate single sub-circuit. This matches much more closely the physical reality of display hardware—a designer might choose to use a series of separate identical seven-segment displays to build a metering circuit, for example.

If the numeric quantity were stored and manipulated as pure binary, interfacing to such a display would require complex circuitry. Therefore, in cases where the calculations are relatively simple, working throughout with BCD can lead to a simpler overall system than converting to and from binary. Most pocket calculators do all their calculations in BCD. The same argument applies when hardware of this type uses an embedded microcontroller or other small processor.

Often, smaller code results when representing numbers internally in BCD format, since a conversion from or to binary representation can be expensive on such limited processors. For these applications, some small processors feature BCD arithmetic modes, which assist when writing routines that manipulate BCD quantities. In packed BCD or simply packed decimaleach of the two nibbles of each byte represent a decimal digit.

Most implementations are big **decode date and time in a sequence of binary data**i. The lower nibble of the rightmost byte is usually used as the sign flag, although some unsigned representations lack a sign flag. As an example, a 4-byte value consists of 8 nibbles, wherein the upper 7 nibbles store the digits decode date and time in a sequence of binary data a 7-digit decimal value and the lowest nibble indicates the sign of the decimal integer value.

Other allowed signs are A and E for positive and B for negative. Most implementations also provide unsigned BCD values with a sign nibble of F. Burroughs systems used D for negative, and any other value is considered a positive sign value the processors will normalize a positive sign to C. No matter how many bytes wide a word is, there are always an even number of nibbles because each byte has two of them.

Note that, like character strings, the first byte of the packed decimal — with the most significant two digits — is usually stored in the lowest address in memory, independent of the endianness of the machine.

The extra storage requirements are usually offset by the need for the accuracy and compatibility with calculator or hand calculation that fixed-point decimal arithmetic provides. Denser packings of BCD exist which avoid the storage penalty and also need no arithmetic operations for common conversions.

Ten's complement representations for negative numbers offer an alternative approach to encoding the sign of packed and other BCD numbers. In this case, positive numbers always have a most significant digit between 0 and 4 inclusivewhile negative numbers are represented by the 10's complement of the corresponding positive number.

As a result, this system allows for, a bit packed BCD numbers to range from , to 49,, and -1 is represented as As with two's complement binary numbers, the range decode date and time in a sequence of binary data not symmetric about zero. These languages allow the programmer to specify an implicit decimal point in front of one of the digits.

The decimal point is not actually stored in memory, as the packed BCD storage format does not provide for it. Its location is simply known to the compiler and the generated code acts accordingly for the various arithmetic operations. If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 2 10 1, is greater than 10 3 1,if three decimal digits are encoded together, only 10 bits are needed.

The latter has the advantage that subsets of the encoding encode two digits in the optimal seven bits and one digit in four bits, as in regular BCD. Some implementations, for example IBM mainframe systems, support zoned decimal numeric representations. Each decimal digit is stored in one byte, with the lower four bits encoding the digit in BCD form.

The upper four bits, called the "zone" bits, are usually set to a fixed value so that the byte holds a character value corresponding to the digit. For signed zoned decimal values, the rightmost least significant zone nibble holds the sign digit, which is the same set of values that are used for signed packed decimal numbers see above.

These characters vary depending on the local character code page setting. The IBM series are character-addressable machines, each location being six bits labeled B, A, 8, 4, 2 and 1, plus an odd parity check bit C and a word mark bit M. For encoding digits 1 through 9B and A are zero and the digit value represented by standard 4-bit BCD in bits 8 through 1.

For most other characters bits B and A are derived simply from the "12", "11", and "0" "zone punches" in the punched card character code, and bits 8 through 1 from the 1 through 9 punches.

A "12 zone" punch set both B and Aan "11 zone" set Band a "0 zone" a 0 punch combined with any others set A. Thus the letter Awhich is 12,1 in the punched card decode date and time in a sequence of binary data, is encoded B,A,1. This allows the circuitry to convert between the punched card format and the internal storage format to be very simple with only a few special cases. One important special case is digit 0represented by a lone 0 punch in the card, and 8,2 in core memory.

The memory of the IBM is organized into 6-bit addressable digits, the usual 8, 4, 2, 1 plus Fused as a flag bit and Can odd parity check decode date and time in a sequence of binary data. BCD alphamerics are encoded using digit pairs, with the "zone" in the even-addressed digit and the "digit" in the odd-addressed digit, the "zone" being related to the 1211and 0 "zone punches" as in the series. A variable length Packed BCD numeric data type is also implemented, providing machine instructions that perform arithmetic directly on packed decimal data.

All of these are used within hardware registers and processing units, and in software. The MicroVAX and later VAX implementations dropped this ability from the CPU but retained code compatibility with earlier machines by implementing the missing instructions in an operating system-supplied software library.

This is invoked automatically via exception handling when the no longer implemented instructions are encountered, so that programs using them can execute without modification on the newer machines. The Intel x86 architecture supports a unique digit ten-byte BCD format that can be loaded into and stored from the floating point registers, and computations can be performed there.

The Motorola series had BCD instructions. In more recent computers such capabilities are almost always implemented in software rather than the CPU's instruction set, but BCD numeric data is still extremely common in commercial and financial applications.

There are tricks for implementing packed BCD and zoned decimal add or subtract operations using short but difficult to understand sequences of word-parallel logic and binary arithmetic operations. Conversion of the simple sum of two digits can be done by adding 6 that is, 16 — 10 when the five-bit result of adding a pair of digits has a value greater decode date and time in a sequence of binary data 9.

Note that is the binary, not **decode date and time in a sequence of binary data,** representation of the desired result. Also note that it cannot fit in a 4-bit number. In BCD as in decimal, there cannot exist a value greater than 9 per digit. To correct this, 6 is added to that sum and then the result is treated as two nibbles:. The two nibbles of the result, andcorrespond to the digits "1" and "7".

This yields "17" in BCD, which is the correct result. This technique can be extended to adding multiple digits by adding in groups from right to left, propagating the second digit as a carry, always comparing the 5-bit result decode date and time in a sequence of binary data each digit-pair sum to 9.

Some CPUs provide a half-carry flag to facilitate BCD arithmetic adjustments following binary addition and subtraction operations. Subtraction is done by adding the ten's complement of the subtrahend.

To represent the sign of a number in BCD, the number is used to represent a positive numberand is used to represent a negative number. The remaining 14 combinations are invalid signs.

To illustrate signed BCD subtraction, consider the following problem: In signed BCD, is The ten's complement of can be obtained by taking the nine's complement ofand then adding decode date and time in a sequence of binary data.

Since BCD is a form of decimal representation, several of the digit sums above are invalid. In the event that an invalid entry any BCD digit greater than exists, 6 is added to generate a carry bit and cause the sum to become a valid entry.

So adding 6 to the invalid entries results in the following:. To check the answer, note that the first digit is 9, which means negative. To check the rest of the digits, represent them in decimal. The binary-coded decimal scheme described in this article is the most common encoding, but there are many others.

The following table represents decimal digits from 0 to 9 in various BCD systems:. In the case Gottschalk v. Bensonthe U. Supreme Court overturned a lower court decision which had allowed a patent for converting BCD encoded numbers to binary on a computer. This was an important case in determining the patentability of software and algorithms. The Atari 8-bit family of computers used BCD to implement floating-point algorithms.

Forms and interpretations of binary data come in different technical and scientific fields. Such two-valued unit can be termed:. A discrete variable that can take only one state contains zero informationand 2 is the next natural number after 1. That is why the bita variable with only two possible values, is a standard primary unit of information.

A collection of n bits may have 2 n states: Number of states of a collection of discrete variables depends exponentially on the number of variables, and only as a power law on number of states of each variable. Ten bits have more states than three decimal digits So, the use of any other small number than 2 does not provide an advantage.

Moreover, Boolean algebra provides a convenient mathematical structure for collection of bits, with a semantic of a collection of propositional variables. Boolean algebra operations are known as " bitwise operations " in computer science. Boolean functions are also well-studied theoretically and easily implementable, either with computer programs or by so-named logic gates in digital electronics. This contributes to the use of bits to represent different data, even those originally not binary.

In statisticsbinary data is a statistical data type described by binary variableswhich can take only two possible values. Binary data represents the outcomes of Bernoulli trials —statistical experiments with only two possible outcomes. It is a type of categorical datawhich more generally represents experiments with a fixed number of possible outcomes. The two values in a binary variable, despite being coded numerically as 0 and 1, are generally considered to exist on a nominal scalemeaning they represent qualitatively different values that cannot be compared numerically.

In this respect, also, binary data is similar to categorical data but distinct from count data or other types of numeric data. Often, binary data is used to represent one of two conceptually opposed values, e. However, it can also be used for data that is assumed to have only two possible values, even if they are not conceptually opposed or conceptually represent all possible values in the space.

For example, binary data decode date and time in a sequence of binary data often used to represent the party choices of voters in elections in the United Statesi. In this case, there is no inherent decode date and time in a sequence of binary data why only two political parties should exist, and indeed, other parties do exist in the U.

Like all discretizationit involves discretization errorbut the goal is to learn something valuable despite the error treating it as negligible for the purpose at hand, but remembering that it cannot be assumed to be negligible in general. Binary variables that are random variables are distributed according to a Bernoulli distribution. Regression analysis on predicted outcomes that are binary variables is accomplished through logistic regressionprobit regression decode date and time in a sequence of binary data a related type of discrete choice model.

In modern computersbinary data refers to any data represented in binary form rather than interpreted on a higher level or converted into some other form. At the lowest level, bits are stored in a bistable device such as a flip-flop. Decode date and time in a sequence of binary data most binary data has symbolic meaning except for don't cares not all binary data is numeric. Some binary data corresponds to computer instructionssuch as the data within processor registers decoded by the control unit along the fetch-decode-execute cycle.

Computers rarely modify individual bits for performance reasons. Instead, data is aligned in groups of a fixed number of bits, usually 1 byte 8 bits. Hence, "binary data" in computers are actually sequences of bytes.

On a higher level, data is accessed in groups of 1 word 4 bytes for bit systems and 2 words for bit systems. In applied computer science and in the information technology field, the term binary data is often specifically opposed to text-based datareferring to any sort of data that cannot be interpreted as text.

However, it often refers specifically to whether the individual bytes of a file are interpretable as text see character encoding decode date and time in a sequence of binary data cannot so be interpreted.

When this last meaning is intended, the more specific terms binary format and text ual format are sometimes used. Note that semantically textual data can be represented in binary format e. From Wikipedia, the free encyclopedia. This article does not cite any sources. Please help improve this article by adding citations to reliable sources.

Unsourced material may be challenged and removed. June Learn how and when to remove this template message. Retrieved from " https: Articles lacking sources from June All articles lacking sources. Views Read Edit View history. This page was last edited on 5 Mayat By using this site, you agree to the Terms of Use and Privacy Policy.

The following sections in this topic provide information about and examples for using the date and time data types and functions. Converting date, time, datetime2, and datetimeoffset. Conversion Between String Literals and time ndate, datetime2 nand datetimeoffset n. Backward Compatibility for Down-level Clients.

String literal formats affect the presentation of data in applications to users but not the underlying integer storage format in SQL Server. Some string literal formats are not affected by these settings. Consider using a format that does not depend on these settings, unless you know the settings are correct for the format. The ISO format does not depend on these settings and is an international standard.

Transact-SQL that uses string literal formats, dependent on system settings, is less portable. To find out the default string literal format for down-level clients, see the topic for each date and time data type. The ydm date format is not supported for the datedatetime2 and datetimeoffset types.

A run time error will be raised. The following table lists different date and time string formats. The ISO formats, 'T Yes datedatetime2datetimeoffset. No datetime2datetimeoffset. You can specify date data as an unseparated string. The decode date and time in a sequence of binary data data can be specified by using four, six, or eight digits, an empty string, or a time value without a date value.

The six-digit or eight-digit strings are always interpreted as ymd. The month and day must always be two digits. A string of only four digits is interpreted as the year. The month and date are set to January 1. When you specify only four digits, you must include the century. To use the ISO format, you must specify each element in the format. This includes the Tthe colons: The brackets indicate that the fractional seconds or time zone offset components are optional.

The advantage in using the ISO format is that it is an international standard. Date and time values that are specified by using this format are unambiguous. You can specify a month as a name, for example, April or the abbreviation Apr in English.

Commas are optional and capitalization is ignored. If you specify only the last two digits of the year, values less than the last two digits of the value of the two digit year cutoff configuration option are in the same century as the cutoff year. Values that are greater than or equal to the value of this option are in the century that comes before the cutoff year.

For example, if two digit year cutoff is default25 is interpreted as and 50 is interpreted as To avoid ambiguity, use four-digit years. The following formats are the valid alphabetical formats for SQL Server date data. Characters that are enclosed in brackets are optional. You can specify date data with a numeric month. This string must appear in the following form:.

If the order does not match the setting, the values are not interpreted as dates, because they are out of range or the values are misinterpreted.

A four-digit year will be interpreted as year. Enclose each format with single quotation marks '. You can specify a suffix of AM or PM to indicate if the time value is before or after 12 noon. The case of AM or PM is ignored.

Hours can be specified by using either a hour or hour clock. The hour values are interpreted as follows. The hour value of 00 represents the hour after midnight AMregardless of whether you specify AM. You cannot specify PM when the hour equals Hour values from 01 through 11 represent the hours before noon if neither AM nor PM is specified. They also represent the hours before noon when AM is specified. They represent hours after noon if PM is specified. The hour value 12 represents the hour that starts at noon if neither AM nor PM is specified.

If AM is specified, it represents the hour that starts at midnight. If PM is specified, it represents the hour that starts at noon. Hour values from 13 through 23 represents hours after noon if AM or PM is not specified. They also represent the hours after noon when PM is specified. You cannot specify AM when the hour value is from 13 through Milliseconds can be preceded by either a colon: If preceded by a colon, the number means thousandths-of-a-second.

If preceded by a period, a single digit means tenths-of-a-second, two digits mean hundredths-of-a-second, and three digits mean thousandths-of-a-second. Specifies the type of the escape sequence. Is the value of the escape sequence. There are two kinds of conversions between different date types: Conversions from string literals to date and time types are permitted if all parts of the strings are in valid formats.

Otherwise, a runtime error is raised. Implicit conversions or explicit conversions that do not specify decode date and time in a sequence of binary data style, from date and time types to string literals will be in the default format of the decode date and time in a sequence of binary data session.

The following table shows the rules for conversion between datetimedecode date and time in a sequence of binary data and datetimeoffset types and string literals. ODBC string literals are mapped to the datetime data type. The fractional seconds precision of datetime has an accuracy of one three-hundredths of a second equivalent to 3.

Values are rounded to increments of. For time 3datetime2 3 or datetimeoffset 3the fractional seconds precision has an accuracy of one millisecond. The tables in this section describe how each of the following date and time data types is converted to the other date and time data types:.

The following table describes what occurs when a date data type is converted to other date and time data types. The conversion fails, and error message is raised: The date is copied. The following code shows the results of converting a date value to a datetime value. When the date value is in the range of a smalldatetimethe date component is copied and the time component is set to When the date value is outside the range of a smalldatetime value, error message is raised: The following code shows the results of converting a date value to a smalldatetime value.

The following code shows the results of converting a date value to a datetimeoffset 3 value. The following code shows the results of converting **decode date and time in a sequence of binary data** date value to a datetime2 3 value.

The following table describes what occurs when a time data type is converted to other date and time data types. The hour, minute, and seconds are copied. When the destination precision is less than the source precision, the fractional seconds will be truncated to fit the destination precision. The following example shows the results of converting a time 4 value to a time 3 value. The hour, minute, and second values are copied; and the date component is set to ''.

When the fractional seconds precision of the time n value is greater than three digits, the datetime result will be truncated. The following code shows the results of converting a time 4 value to a datetime value. The date is set to '', and the hour and minute values are copied. The seconds and fractional seconds are set to 0. The following code shows the results of converting a time 4 value to a smalldatetime value.

The date is set to '', and the time is copied. When the fractional seconds precision of the time n value is greater than the precision of the datetimeoffset n value, the value is truncated to fit. Decode date and time in a sequence of binary data following example shows the results of converting a time 4 value to a datetimeoffset 3 type.

The date is set to '', the time component is copied, and the time zone offset is set to When the fractional seconds precision of the datetime2 n value is greater than the time n value, the value decode date and time in a sequence of binary data be truncated to fit.

The following example shows the results of converting a time 4 value to a datetime2 2 value. The following table describes what occurs when a datetime data type is converted to other date and time data types.

The time component is copied, and the date component is set to ''. When the fractional precision of the time n value greater than three digits, the value will be truncated to fit.