可能重复:
明确,外行解释|之间的区别 和|| 在c#?
与|比较有什么区别 和|| 在C#和Javascript中使用&&和&&?
例子:
if(test == test1 | test1 == test2) or if(test == test1 || test1 == test2)
if(test == test1 & test1 == test2) or if(test == test1 && test1 == test2)
Run Code Online (Sandbox Code Playgroud) 如果我有两个byte
小号a
和b
,怎么来的:
byte c = a & b;
Run Code Online (Sandbox Code Playgroud)
关于将byte转换为int会产生编译器错误?它这样做,即使我把一个显式类型转换前面a
和b
.
另外,我知道这个问题,但我真的不知道它是如何应用的.这似乎是一个返回类型的问题operator &(byte operand, byte operand2)
,编译器应该能够像任何其他运算符一样进行排序.
任何人都可以解释为什么以下不编译?
byte b = 255 << 1
Run Code Online (Sandbox Code Playgroud)
错误:
常量值'510'无法转换为'字节'
我期待二进制中的以下内容:
1111 1110
Run Code Online (Sandbox Code Playgroud)
类型转换困扰了我.
考虑以下代码:
public class ShortDivision {
public static void main(String[] args) {
short i = 2;
short j = 1;
short k = i/j;
}
}
Run Code Online (Sandbox Code Playgroud)
编译它会产生错误
ShortDivision.java:5: possible loss of precision
found : int
required: short
short k = i/j;
Run Code Online (Sandbox Code Playgroud)
因为表达式i/j的类型显然是int,因此必须强制转换为short.
为什么类型i/j
不短?
我在C#编译器中发现了奇怪的情况.为什么需要下面的演员?
using System;
class Program
{
private const byte BIT_ZERO_SET = 1;
private const byte BIT_ONE_SET = 2;
private const byte BIT_TWO_SET = 4;
static void Main(string[] args)
{
byte b = BIT_ZERO_SET | BIT_ONE_SET;
Console.WriteLine(b);
//Does not compile, says needs to cast to int.
//b = b | BIT_TWO_SET;
//Compiles...ugly
b = (byte)(b | BIT_TWO_SET);
Console.WriteLine(b);
Console.WriteLine("Press enter.");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.