ููŠ ู„ุบุฉ C# ูˆุงู„ู„ุบุงุช ุงู„ู€ Low-levelุŒ ุงู„ู€ Data ุจุชุชุฎุฒู† ุฌูˆู‡ ุงู„ู€ Memory ุนู„ู‰ ุดูƒู„ Binary Representation (ูŠุนู†ูŠ ุฃุตูุงุฑ ูˆูˆุญุงูŠุฏ). ูƒู„ Data Type ู„ูŠู‡ ุทุฑูŠู‚ุฉ ู…ุนูŠู†ุฉ ููŠ ุงู„ุชุฎุฒูŠู† ุจุชุญุฏุฏ ู‡ูˆ ู‡ูŠุงุฎุฏ ูƒุงู… Bit ููŠ ุงู„ุฐุงูƒุฑุฉุŒ ูˆู‡ู„ ููŠู‡ Bit ู…ู†ู‡ู… ู…ุฎุตุต ุนุดุงู† ูŠุญุฏุฏ ุงู„ุฅุดุงุฑุฉ (sign) ูˆู„ุง ู„ุฃ.

1. Signed vs. Unsigned

ู„ู…ุง ุจู†ุชูƒู„ู… ุนู† ุงู„ุฃุฑู‚ุงู… ุงู„ุตุญูŠุญุฉ (Integers)ุŒ ุจู†ู„ุงู‚ูŠ ู†ูˆุนูŠู† ุฃุณุงุณูŠูŠู† ู…ู†ู‡ู…:

  • ุงู„ู€ (Signed): ุฏูŠ ุงู„ู€ Types ุงู„ู„ูŠ ู…ู…ูƒู† ุชุดูŠู„ ุฃุฑู‚ุงู… ู…ูˆุฌุจุฉ ุฃูˆ ุณุงู„ุจุฉ ุฃูˆ ุตูุฑ.
  • ุงู„ู€ (Unsigned): ุฏูŠ ุงู„ู€ Types ุงู„ู„ูŠ ุจุชุดูŠู„ ุฃุฑู‚ุงู… ู…ูˆุฌุจุฉ ุจุณ (ุตูุฑ ุฃูˆ ุฃูƒุชุฑ ู…ู† ุงู„ุตูุฑ).

Sign Bit

  • ููŠ ุงู„ุฃุฑู‚ุงู… ุงู„ู€ SignedุŒ ุจูŠูƒูˆู† ููŠู‡ Bit ูˆุงุญุฏ ุจุณ (ุบุงู„ุจู‹ุง ุจูŠูƒูˆู† ุฃูˆู„ Bit ุนู„ู‰ ุงู„ุดู…ุงู„ุŒ ุงู„ู„ูŠ ุจู†ุณู…ูŠู‡ Most Significant Bit - MSB) ู…ุญุฌูˆุฒ ู…ุฎุตูˆุต ุนุดุงู† ูŠู‚ูˆู„ู†ุง ุฅุดุงุฑุฉ ุงู„ุฑู‚ู… ุฅูŠู‡:

    • ู„ูˆ ุงู„ู€ Bit ุฏู‡ ู‚ูŠู…ุชู‡ 0: ูŠุจู‚ู‰ ุงู„ุฑู‚ู… ุฏู‡ ู…ูˆุฌุจ (ุฃูˆ ุตูุฑ).
    • ู„ูˆ ุงู„ู€ Bit ุฏู‡ ู‚ูŠู…ุชู‡ 1: ูŠุจู‚ู‰ ุงู„ุฑู‚ู… ุฏู‡ ุณุงู„ุจ.
  • ูƒู…ุซุงู„ุŒ ุงู„ู€ Data Type ุงู„ู„ูŠ ุงุณู…ู‡ int ููŠ C# ุจูŠุงุฎุฏ 4 ุจุงูŠุช (ูŠุนู†ูŠ 32 Bit). ุฃูˆู„ Bit ู…ู†ู‡ู… ุจูŠุณุชุฎุฏู… ุนุดุงู† ูŠุญุฏุฏ ุงู„ุฅุดุงุฑุฉุŒ ูˆุงู„ู€ 31 Bit ุงู„ุจุงู‚ูŠูŠู† ุจูŠุณุชุฎุฏู…ูˆุง ุนุดุงู† ูŠู…ุซู„ูˆุง ู‚ูŠู…ุฉ ุงู„ุฑู‚ู… ู†ูุณู‡.

  • ุงู„ู‚ูŠู… ุงู„ู…ูˆุฌุจุฉ: ุจุชุชู…ุซู„ ููŠ ุงู„ู€ Binary Representation ุจุดูƒู„ ุทุจูŠุนูŠ ูˆู…ูู‡ูˆู….

  • ุงู„ู‚ูŠู… ุงู„ุณุงู„ุจุฉ: ุจุชุชู…ุซู„ ููŠ ุงู„ู€ Memory ุจุงุณุชุฎุฏุงู… ุทุฑูŠู‚ุฉ ู…ุดู‡ูˆุฑุฉ ุฌุฏู‹ุง ุงุณู…ู‡ุง Twoโ€™s Complement.

Twoโ€™s Complement

  • ุฏูŠ ุงู„ุชู‚ู†ูŠุฉ ุงู„ู„ูŠ ุจุชุณุชุฎุฏู… ุจุดูƒู„ ูˆุงุณุน ุฌุฏู‹ุง ุนุดุงู† ู†ู…ุซู„ ุงู„ุฃุฑู‚ุงู… ุงู„ุณุงู„ุจุฉ ุฌูˆู‡ ุงู„ูƒู…ุจูŠูˆุชุฑ.
  • ููƒุฑุชู‡ุง ุจุจุณุงุทุฉ ูƒุฏู‡: ุนุดุงู† ุชุฌูŠุจ ุงู„ู€ Twoโ€™s Complement ู„ุฑู‚ู… ุณุงู„ุจ (ุฒูŠ -5 ู…ุซู„ุงู‹):
    1. ู‡ุงุช ุงู„ู€ Binary Representation ุจุชุงุน ุงู„ุฑู‚ู… ุงู„ู…ูˆุฌุจ ุงู„ู…ู‚ุงุจู„ ู„ูŠู‡ (ูŠุนู†ูŠ ู‡ุงุช ุงู„ู€ Binary ุจุชุงุน 5).
    2. ุงู‚ู„ุจ ูƒู„ ุงู„ู€ Bits ุงู„ู„ูŠ ุทู„ุนุช ู…ุนุงูƒ (ุงู„ู€ 0 ูŠุจู‚ู‰ 1 ูˆุงู„ู€ 1 ูŠุจู‚ู‰ 0). ุฏูŠ ุงุณู…ู‡ุง Oneโ€™s Complement.
    3. ุงุฌู…ุน 1 ุนู„ู‰ ุงู„ู†ุงุชุฌ ุงู„ู„ูŠ ุทู„ุนู„ูƒ ุจุนุฏ ุฎุทูˆุฉ ุงู„ู‚ู„ุจ ุฏูŠ.
  • ุงู„ู…ูŠุฒุฉ ุงู„ูƒุจูŠุฑุฉ ุฃูˆูŠ ููŠ ุงู„ุทุฑูŠู‚ุฉ ุฏูŠ ุฅู†ู‡ุง ุจุชุฎู„ูŠ ุนู…ู„ูŠุงุช ุงู„ุฌู…ุน ูˆุงู„ุทุฑุญ ุชุดุชุบู„ ุจู†ูุณ ุงู„ุฏูˆุงูŠุฑ ุงู„ุฅู„ูƒุชุฑูˆู†ูŠุฉ (hardware circuits) ุงู„ู„ูŠ ุฌูˆู‡ ุงู„ู€ ProcessorุŒ ุณูˆุงุก ูƒุงู†ุช ุงู„ุฃุฑู‚ุงู… ุงู„ู„ูŠ ุจุชุฌู…ุนู‡ุง ุฃูˆ ุจุชุทุฑุญู‡ุง ุฏูŠ ู…ูˆุฌุจุฉ ุฃูˆ ุณุงู„ุจุฉ. ูˆุฏู‡ ุจูŠุณู‡ู„ ูƒุชูŠุฑ ููŠ ุชุตู…ูŠู… ุงู„ู€ Processors.

2. Practical Examples

2.1. Signed

// Example with signed int
int x = -5;
 
// In memory, -5 is stored using Two's Complement representation.
// For a 32-bit integer, it would look something like:
// 1111 1111 1111 1111 1111 1111 1111 1011
// The first bit (Most Significant Bit) is 1, indicating a negative number.
Console.WriteLine($"The value of signed int x is: {x}");

2.2. Unsigned

// Example with unsigned uint
uint y = 5;
 
// In memory, 5 is stored directly in its binary form.
// All 32 bits are used to represent the magnitude of the number,
// as unsigned types cannot be negative.
// For a 32-bit unsigned integer, it would look like:
// 0000 0000 0000 0000 0000 0000 0000 0101
Console.WriteLine($"The value of unsigned int y is: {y}");
 
// Trying to assign a negative value to an unsigned type causes an error
// uint z = -10; // Compile-time error

3. Additional Details

  • ุจุงู„ู†ุณุจุฉ ู„ู„ู€ Types ุงู„ู„ูŠ ุจุชุดูŠู„ ุฃุฑู‚ุงู… ุจูƒุณูˆุฑ (Floating-Point Types) ุฒูŠ ุงู„ู€ float ูˆุงู„ู€ doubleุŒ ุทุฑูŠู‚ุฉ ุงู„ุชุฎุฒูŠู† ุจุชุงุนุชู‡ุง ู…ุฎุชู„ูุฉ ุนู† ุงู„ู€ Integers. ุจูŠูƒูˆู† ููŠู‡ Bits ู…ุฎุตุตุฉ ู„ู„ุฅุดุงุฑุฉุŒ ูˆBits ุชุงู†ูŠุฉ ู„ู„ู‚ูŠู…ุฉ ุงู„ู„ูŠ ููˆู‚ (ุงู„ู„ูŠ ุจู†ุณู…ูŠู‡ุง exponent)ุŒ ูˆBits ุชุงู†ูŠุฉ ู„ู„ู‚ูŠู…ุฉ ุงู„ูƒุณุฑูŠุฉ ู†ูุณู‡ุง (ุจู†ู‚ูˆู„ ุนู„ูŠู‡ุง mantissa ุฃูˆ significand). ุงู„ุชุฎุฒูŠู† ุฏู‡ ุจูŠูƒูˆู† ู…ุงุดูŠ ุนู„ู‰ ู…ุนูŠุงุฑ ุนุงู„ู…ูŠ ุงุณู…ู‡ IEEE 754.
  • ุฃู…ุง ุงู„ู€ Types ุงู„ู„ูŠ ู…ู† ู†ูˆุน ู…ุฑุฌุน (Reference Types) ุฒูŠ ุงู„ู€ Classes ูˆุงู„ู€ ArraysุŒ ูุฏูŠ ู…ุด ุจุชุฎุฒู† ุงู„ู‚ูŠู…ุฉ ู†ูุณู‡ุง ุจู†ูุณ ุงู„ุทุฑูŠู‚ุฉ ุฏูŠ. ุงู„ู€ Variable ุงู„ู„ูŠ ุจุชุนุฑูู‡ ู…ู†ู‡ู… ุจูŠุฎุฒู† ู…ุฌุฑุฏ ุนู†ูˆุงู† (reference) ุจูŠุดุงูˆุฑ ุนู„ู‰ ุงู„ู…ูƒุงู† ุงู„ู„ูŠ ููŠู‡ ุงู„ู€ Data ุงู„ูุนู„ูŠุฉ ุฏูŠ ููŠ ุงู„ู€ Heap.

ูู‡ู… ุฅุฒุงูŠ ุงู„ุฃุฑู‚ุงู… ุจุชุชู…ุซู„ ุฌูˆู‡ ุงู„ู€ Memory ุจูŠุณุงุนุฏูƒ ุชูู‡ู… ุญุฏูˆุฏ ูƒู„ Data Type ุฅูŠู‡ุŒ ูˆู„ูŠู‡ ู…ู…ูƒู† ุชุญุตู„ ู…ุดุงูƒู„ ุฒูŠ ุงู„ู€ Overflow (ู„ู…ุง ุงู„ุฑู‚ู… ูŠูƒุจุฑ ุฃูƒุชุฑ ู…ู† ุงู„ู„ูŠ ุงู„ู€ Type ูŠู‚ุฏุฑ ูŠุดูŠู„ู‡) ููŠ ุจุนุถ ุงู„ุฃูˆู‚ุงุช.