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
Convert the specified double-precision floating point number to a 64-bit signed integer in C#
To convert a double-precision floating point number to its 64-bit signed integer representation, C# provides the BitConverter.DoubleToInt64Bits() method. This method converts the binary representation of a double value into a long integer without changing the underlying bit pattern.
This is useful when you need to examine the internal binary structure of floating-point numbers or perform low-level operations on their bit representations.
Syntax
Following is the syntax for converting a double to its 64-bit signed integer representation −
long result = BitConverter.DoubleToInt64Bits(doubleValue);
Parameters
The DoubleToInt64Bits() method takes one parameter −
- value: A double-precision floating point number to convert.
Return Value
Returns a 64-bit signed integer whose bit pattern is equivalent to the input double value.
Using DoubleToInt64Bits() with Positive Values
Example
using System;
public class Demo {
public static void Main() {
double d = 5.646587687;
Console.WriteLine("Original double 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 to double: " + backToDouble);
}
}
The output of the above code is −
Original double value: 5.646587687 64-bit signed integer: 4618043510978159912 Converted back to double: 5.646587687
Using DoubleToInt64Bits() with Small Values
Example
using System;
public class Demo {
public static void Main() {
double d = 0.001;
Console.WriteLine("Original double value: " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.WriteLine("64-bit signed integer: " + res);
// Show hexadecimal representation for better understanding
Console.WriteLine("Hexadecimal representation: 0x" + res.ToString("X"));
}
}
The output of the above code is −
Original double value: 0.001 64-bit signed integer: 4562254508917369340 Hexadecimal representation: 0x3F50624DD2F1A9FC
Using DoubleToInt64Bits() with Special Values
Example
using System;
public class Demo {
public static void Main() {
double[] specialValues = { 0.0, -0.0, double.PositiveInfinity,
double.NegativeInfinity, double.NaN };
foreach (double value in specialValues) {
long bits = BitConverter.DoubleToInt64Bits(value);
Console.WriteLine($"Value: {value,18} -> 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
How It Works
The DoubleToInt64Bits() method performs a direct bit-level conversion without any mathematical transformation. The IEEE 754 double-precision format uses 64 bits arranged as follows −
- 1 bit for the sign
- 11 bits for the exponent
- 52 bits for the mantissa (fractional part)
This method is particularly useful for debugging floating-point issues, implementing custom serialization, or working with bit manipulation algorithms.
Conclusion
The BitConverter.DoubleToInt64Bits() method converts a double-precision floating point number to its 64-bit signed integer bit representation. This is useful for examining the internal structure of floating-point numbers and performing low-level bit operations without losing precision.
