现在的位置: 首页 > 综合 > 正文

【C#笔记】探究移位运算符”>>”

2013年05月04日 ⁄ 综合 ⁄ 共 983字 ⁄ 字号 评论关闭

              探究移位运算符">>"

 

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YiWei
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
short val = 38;

            //byte result1 = val >> 1;
            
//Console.WriteLine(result1);
            /* 错误 1 无法将类型“int”隐式转换为“byte”。
             * 存在一个显式转换(是否缺少强制转换?) 
             * D:\c#\YiWei\YiWei\Program.cs 14 28 YiWei
*/

            //short result2 = val >> 1;
            
//Console.WriteLine(result2);
            /*错误 1 无法将类型“int”隐式转换为“short”。
             * 存在一个显式转换(是否缺少强制转换?) 
             * D:\c#\YiWei\YiWei\Program.cs 21 23 YiWei
*/

            int result3 = val >> 1;
            Console.WriteLine(result3);

            long result4 = val >> 1;
            Console.WriteLine(result4);

            Console.WriteLine(val>>1);

            Console.ReadKey();
        }
    }
}

 

 从以上代码来看,移位运算 ">>" 的val>>1的表达式结果是int型的值。
一般的类型都可以做位运算">>" ,但是结果是int型值。这个结果可以隐式转换为其他类型。

2009-12-26 15:22:05

抱歉!评论已关闭.