Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
BitConverter.DoubleToInt64Bits() Method in C#
The BitConverter.DoubleToInt64Bits() method in C# converts a double-precision floating-point number to its 64-bit signed integer binary representation. This method is useful for examining the underlying binary structure of floating-point numbers or for low-level operations that require access to the raw bits.
Syntax
Following is the syntax for the DoubleToInt64Bits() method −
public static long DoubleToInt64Bits(double value);
Parameters
value − The double-precision floating-point number to convert.
Return Value
Returns a 64-bit signed integer whose value is equivalent to the binary representation of the input double value.
Example 1: Converting Positive Double
using System;
public class Demo {
public static void Main() {
double d = 5.646587687;
Console.WriteLine("Value = " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.WriteLine("64-bit signed integer = " + res);
// Convert back to verify
double backToDouble = BitConverter.Int64BitsToDouble(res);
Console.WriteLine("Converted back = " + backToDouble);
}
}
The output of the above code is −
Value = 5.646587687 64-bit signed integer = 4618043510978159912 Converted back = 5.646587687
Example 2: Converting Small Double Values
using System;
public class Demo {
public static void Main() {
double d = 0.001;
Console.WriteLine("Value = " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.WriteLine("64-bit signed integer = " + res);
// Display in hexadecimal for better understanding
Console.WriteLine("Hexadecimal representation = 0x" + res.ToString("X"));
}
}
The output of the above code is −
Value = 0.001 64-bit signed integer = 4562254508917369340 Hexadecimal representation = 0x3F50624DD2F1A9FC
Example 3: Special Double Values
using System;
public class Demo {
public static void Main() {
// Test special double values
double[] values = { 0.0, -0.0, Double.PositiveInfinity,
Double.NegativeInfinity, Double.NaN };
foreach (double value in values) {
long bits = BitConverter.DoubleToInt64Bits(value);
Console.WriteLine($"Value: {value,20} | Bits: {bits,20}");
}
}
}
The output of the above code is −
Value: 0 | Bits: 0 Value: 0 | Bits: -9223372036854775808 Value: Infinity | Bits: 9218868437227405312 Value: -Infinity | Bits: -4503599627370496 Value: NaN | Bits: -2251799813685248
Common Use Cases
Binary analysis − Examining the IEEE 754 binary representation of floating-point numbers.
Serialization − Converting doubles to integers for storage or transmission.
Hash functions − Using the bit pattern of doubles in hash calculations.
Low-level operations − Performing bitwise operations on floating-point data.
Conclusion
The BitConverter.DoubleToInt64Bits() method provides access to the raw binary representation of double-precision floating-point numbers as 64-bit signed integers. This method is essential for low-level programming tasks and understanding the underlying structure of IEEE 754 floating-point values.
