C#-এ BitConverter.DoubleToInt64Bits() মেথডটি একটি ডাবল-প্রিসিশন ফ্লোটিং-পয়েন্ট সংখ্যা (double) কে এর IEEE 754 বিট প্যাটার্ন হিসেবে ৬৪-বিট সাইনড ইন্টিজার (long) এ রূপান্তর করে। এটি Int64BitsToDouble()-এর বিপরীত অপারেশন।
সিনট্যাক্স (Syntax)
public static long DoubleToInt64Bits(double value);
উদাহরণ ১: সাধারণ ডাবল মান
using System;
public class Demo
{
public static void Main()
{
double d = 5.646587687;
Console.Write("Value = " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.Write("\n64-bit signed integer = " + res);
}
}
আউটপুট
Value = 5.646587687 64-bit signed integer = 4618043510978159912
উদাহরণ ২: ছোট ডাবল মান
using System;
public class Demo
{
public static void Main()
{
double d = 0.001;
Console.Write("Value = " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.Write("\n64-bit signed integer = " + res);
}
}
আউটপুট
Value = 0.001 64-bit signed integer = 4562254508917369340
IEEE 754 ডাবল-প্রিসিশন ফরম্যাট
৬৪-বিট ডাবলের বিট বিন্যাস:
- সাইন বিট (১ বিট): বিট ৬৩ (০=পজিটিভ, ১=নেগেটিভ)
- এক্সপোনেন্ট (১১ বিট): বিট ৫২-৬২ (বায়াস = ১০২৩)
- ম্যান্টিসা (৫২ বিট): বিট ০-৫১ (ইমপ্লিসিট লিডিং ১)
বিট প্যাটার্ন উদাহরণ: 5.646587687
Binary: 0 10000000001 011010101111001101000011111101101100110011001100110 Hex: 0x4016AB3D0F3CCCCC Decimal: 4618043510978159912
Round-trip পরীক্ষা (Int64BitsToDouble)
using System;
public class RoundTripDemo
{
public static void Main()
{
double[] testValues = { 0.0, 1.0, -1.0, 3.14159, double.MaxValue, double.MinValue, double.Epsilon, double.NaN, double.PositiveInfinity, double.NegativeInfinity };
Console.WriteLine($"{"Original",15} {"As Int64",22} {"Restored",15} {"Match"}");
Console.WriteLine(new string('-', 70));
foreach (double d in testValues)
{
long bits = BitConverter.DoubleToInt64Bits(d);
double restored = BitConverter.Int64BitsToDouble(bits);
bool match = (d == restored) || (double.IsNaN(d) && double.IsNaN(restored));
Console.WriteLine($"{d,15} {bits,22} {restored,15} {match}");
}
}
}
বিশেষ মানের বিট প্যাটার্ন
| মান | হেক্সাডেসিম্যাল | বর্ণনা |
|---|---|---|
| +0.0 | 0x0000000000000000 | সকল বিট ০ |
| -0.0 | 0x8000000000000000 | সাইন বিট ১, বাকি ০ |
| +Infinity | 0x7FF0000000000000 | এক্সপোনেন্ট সব ১, ম্যান্টিসা ০ |
| -Infinity | 0xFFF0000000000000 | সাইন ১, এক্সপোনেন্ট সব ১ |
| NaN | 0x7FF8000000000000* | এক্সপোনেন্ট সব ১, ম্যান্টিসা নন-জিরো |
প্রয়োগিক ব্যবহার: বাইনারি সিরিয়ালাইজেশন
using System;
using System.IO;
public class BinarySerializer
{
public static void WriteDouble(BinaryWriter writer, double value)
{
long bits = BitConverter.DoubleToInt64Bits(value);
writer.Write(bits); // ৮ বাইট লিখে
}
public static double ReadDouble(BinaryReader reader)
{
long bits = reader.ReadInt64();
return BitConverter.Int64BitsToDouble(bits);
}
public static void Main()
{
using (var ms = new MemoryStream())
using (var writer = new BinaryWriter(ms))
using (var reader = new BinaryReader(ms))
{
double[] data = { 3.14, 2.71, -1.5, 0.0 };
foreach (var d in data)
WriteDouble(writer, d);
ms.Position = 0;
Console.WriteLine("Deserialized:");
while (ms.Position < ms.Length)
Console.WriteLine(ReadDouble(reader));
}
}
}
নোট: এই মেথডটি এন্ডিয়ানেস (endianness) বিবেচনা করে না - এটি রা বিট প্যাটার্ন ফেরত দেয়। নেটওয়ার্ক ট্রান্সমিশনে IPAddress.HostToNetworkOrder ব্যবহার করুন।