From 5109aeb780c94730d91556fc5e162fa6b7785dc5 Mon Sep 17 00:00:00 2001 From: nyaasuki Date: Sat, 14 Dec 2024 18:13:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=8D=E8=BF=90=E7=AE=97=E8=A7=A3=E6=83=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Q&A/关于位运算计算1向右移1位时的问题解惑.md | 309 ++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 Q&A/关于位运算计算1向右移1位时的问题解惑.md diff --git a/Q&A/关于位运算计算1向右移1位时的问题解惑.md b/Q&A/关于位运算计算1向右移1位时的问题解惑.md new file mode 100644 index 0000000..fa4baa0 --- /dev/null +++ b/Q&A/关于位运算计算1向右移1位时的问题解惑.md @@ -0,0 +1,309 @@ +# Q +ִŻУͨԶ򵥵ij˳ 2 ݴηתΪλ㡣ˣںܶ£ֶʹλڸ߲εӦôС ôC#DzǼ12ʱõĴ𰸣 +# A +## 1. C#еij (`/`) Ϊ + +C#У`/` Ϊȡڲ͡Ҫ + +### 1.1 + +ͣ `int``long``short` ȣʱ`/` ִ****Ҳһ**Сֻᱻض**롣 + +**ʾ** + +```csharp +int a = 1; +int b = 2; +int result = a / b; +Console.WriteLine(result); // : 0 +``` + +**ͣ** +- `1 / 2` е `0.5`ڽ`0.5` ضΪ `0` + +### 1.2 + +һǸͣ `float``double``decimal` ȣʱ`/` ִ****С֡ + +**ʾ** + +```csharp +double a = 1.0; +double b = 2.0; +double result = a / b; +Console.WriteLine(result); // : 0.5 +``` + +**ͣ** +- `1.0 / 2.0` `0.5`ҽ `double`Сֱ + +## 2. λ (`>>`) + +λ `>>` 㣬2ʱ˵`number >> n` ͬ `number / (2^n)`****IJ + +### 2.1 ʾ + +```csharp +int number = 4; +int shifted = number >> 1; // ൱ 4 / 2 = 2 +Console.WriteLine(shifted); // : 2 +``` + +### 2.2 `number` Ϊ `1` ʱ + +```csharp +int number = 1; +int shifted = number >> 1; // ൱ 1 / 2 = 0 () +Console.WriteLine(shifted); // : 0 +``` + +**ͣ** +- `1 >> 1` `1` Ķλƶһλõ `0` +- `1 / 2` Ľһ£ `0` + +## 3. ŻӰ + +### 3.1 Ż + +ִC#ڱлԴиŻִЧʡڣ + +- **Ż**˷תΪλ㣬ڲ2ݴηʱ +- **Ż**СĴֱӲôٺõĿ +- **ѭչ**ѭƵĿͨչѭܡ + +### 3.2 ԳľŻ + +⵽2ݴηʱܻὫ `\` 滻ΪӦ `>>`ΪλڵײִЧ + +**ʾ** + +```csharp +int number = 8; +int result = number / 2; // ܱŻΪ number >> 1 +``` + +**ͣ** +- ϴУ`number / 2` ܱŻΪ `number >> 1`ΪǵЧҸЧ + +### 3.3 ɴ + +**ؼ****Żı**ʹ `/` `>>` ͬģͣ˲ + +**֤ȷ** +- Żĺԭ֮һ**ıΪ**ŻĴԭʼ߼ϲͬĽ + +## 4. ص⣺`1 / 2` Ƿᵼ´Ĵ𰸣 + +### 4.1 + +```csharp +int a = 1; +int b = 2; +int result = a / b; +Console.WriteLine(result); // : 0 +``` + +- ****ԤڵΪУ`1 / 2` `0`Ϊ롣ʹλ㣺 + +```csharp +int shifted = a >> 1; // 1 >> 1 = 0 +Console.WriteLine(shifted); // : 0 +``` + +- һ£ `0`ˣⲢǴ󣬶ǷԤΪ + +### 4.2 + +```csharp +double a = 1.0; +double b = 2.0; +double result = a / b; +Console.WriteLine(result); // : 0.5 +``` + +- ****С֣Ϊ `0.5`ȷԤڵĽ + +### 4.3 ܽ + +- ** (`int / int`)**Ϊ`1 / 2` `0`ԤΪ +- ** (`double / double`)**С`1.0 / 2.0` `0.5`ԤΪ +- **Ż**滻Ϊλı + +## 5. ʵӦеĽ + +### 5.1 ʹȷ + +ҪĽѡʵͣ + +- ҪС֣ʹøͣ `float``double``decimal` +- λ㣬ȷʹ͡ + +### 5.2 Ϊ + +ȷ͸𣬱Ͳƥ䵼µ + +### 5.3 ҪȵıŻ + +ִdz죬ŻԻȷһ¡ֶʹλŻͨԵΪѾŻ + +**ʾԱ** + +```csharp +using System; + +class DivisionVsShiftDemo +{ + static void Main() + { + int number = 1; + + // ʹó + int divided = number / 2; + + // ʹλ + int shifted = number >> 1; + + Console.WriteLine($"1 / 2 = {divided}"); // : 0 + Console.WriteLine($"1 >> 1 = {shifted}"); // : 0 + + // һ + } +} +``` + +**** +``` +1 / 2 = 0 +1 >> 1 = 0 +``` + +### 5.4 ܿ + +ڴ߲ӦУ**ܲ΢΢**ȿǴĿɶԺͿάԣֻȷʵƿʱſǽеͼŻ + +## 6. ʵʾ + +### 6.1 λ + +```csharp +using System; + +class IntegerDivisionAndShift +{ + static void Main() + { + int number = 1; + + // + int divided = number / 2; + + // λ + int shifted = number >> 1; + + Console.WriteLine($": {number} / 2 = {divided}"); // : 0 + Console.WriteLine($"λ: {number} >> 1 = {shifted}"); // : 0 + } +} +``` + +**** +``` +: 1 / 2 = 0 +λ: 1 >> 1 = 0 +``` + +### 6.2 + +```csharp +using System; + +class FloatingDivision +{ + static void Main() + { + double number = 1.0; + double result = number / 2.0; + Console.WriteLine($": {number} / 2.0 = {result}"); // : 0.5 + } +} +``` + +**** +``` +: 1 / 2.0 = 0.5 +``` + +### 6.3 ۺʾ + +```csharp +using System; + +class ComprehensiveDemo +{ + static void Main() + { + int[] numbers = { 16, 8, 4, 2, 1 }; + + foreach (var number in numbers) + { + int divided = number / 2; + int shifted = number >> 1; + + Console.WriteLine($": {number}"); + Console.WriteLine($": {number} / 2 = {divided}"); + Console.WriteLine($"λ: {number} >> 1 = {shifted}"); + Console.WriteLine(new string('-', 30)); + } + + // ʾ + double floatNumber = 1.0; + double floatResult = floatNumber / 2.0; + Console.WriteLine($": {floatNumber} / 2.0 = {floatResult}"); + } +} +``` + +**** +``` +: 16 +: 16 / 2 = 8 +λ: 16 >> 1 = 8 +------------------------------ +: 8 +: 8 / 2 = 4 +λ: 8 >> 1 = 4 +------------------------------ +: 4 +: 4 / 2 = 2 +λ: 4 >> 1 = 2 +------------------------------ +: 2 +: 2 / 2 = 1 +λ: 2 >> 1 = 1 +------------------------------ +: 1 +: 1 / 2 = 0 +λ: 1 >> 1 = 0 +------------------------------ +: 1 / 2.0 = 0.5 +``` + +**** +- `16``8``4``2`λõͬĽ +- `1`߾õ `0`ԤڵĽ +- ڸ `1.0`õ `0.5`С֡ + +## 7. + +- **һ**C#Уʹλ `>>` `\`2ʱᵼ´ĽϵĽһµġ + +- **ԤΪ**`1 / 2` ͨ `0` `1.0 / 2.0` ͨ `0.5`ǷѧͱԵԤΪڴ + +- **Ż**ִܵŻ򵥵㣬ЩŻı߼ͽˣصıŻᵼ³Ĵ𰸡 + +- **ʵ** + - **ѡʵ**ҪĽѡ򸡵͡ + - **ȿǿɶ**ܹؼӦУӦʹ `/`ִĿɶԺͿάԡ + - **Ϊ**ղͬΪͲƥ + +иC#ΪŻ⣬ӭʣ \ No newline at end of file