按位运算符|和+一样?

时间:2019-02-21 17:30:27

标签: java

看着|,它被描述为按位运算符OR。 因此,在此代码示例中:

private int getColorRGB(int color) { // 255255255 would be white, 000255000 green, etc.

    if (color < 0) return -1;

    int r = color / 1000000 % 1000;
    int g = color / 1000 % 1000;
    int b = color % 1000;

    if (r > 255 || g > 255 || b > 255) throw new IllegalArgumentException("RGB values cannot exceed 255.");

    return (r >> 16) | (g >> 8) | b; // POINT OF INTEREST
}

我可以用|将标记为POINT OF INTEREST的行中的2 +替换为rrrgggbbb

该方法采用整数200200200,因此255为蓝色,function Update-program { Clear-Host Write-Host "Updating Program..." Write-Host " " if ( -not (Test-Path "$downloadLocation\Here" -PathType Container)) { New-Item -Path "$downloadLocation\Here" -ItemType directory -Force } $apiUrl = 'https://ci.appveyor.com/api' $headers = @{ "Authorization" = "Bearer $token" "Content-type" = "application/json" } $accountName = 'Hello' $projectSlug = 'World' # get project with last build details $project = Invoke-RestMethod -Method Get -Uri "$apiUrl/projects/$accountName/$projectSlug" -Headers $headers # we assume here that build has a single job # get this job id $jobId = $project.build.jobs[0].jobId # get job artifacts (just to see what we've got) $artifacts = Invoke-RestMethod -Method Get -Uri "$apiUrl/buildjobs/$jobId/artifacts" -Headers $headers # here we just take the first artifact, but you could specify its file name # $artifactFileName = 'MyWebApp.zip' $artifactFileName = $artifacts[0].fileName # artifact will be downloaded as $localArtifactPath = "$downloadLocation\$artifactFileName" # download artifact # -OutFile - is local file name where artifact will be downloaded into # the Headers in this call should only contain the bearer token, and no Content-type, otherwise it will fail! Invoke-RestMethod -Method Get -Uri "$apiUrl/buildjobs/$jobId/artifacts/$artifactFileName" ` -OutFile $localArtifactPath -Headers @{ "Authorization" = "Bearer $token" } Clear-Host Write-Host "Download complete, extracting..." Write-Host " " Expand-Archive -LiteralPath "$downloadLocation\$artifactFileName" -DestinationPath "$ProgramDir" -Force Clear-Host Write-Host "Program updated" Write-Host " " 为浅灰色,等等。

所以,我的问题是;两者有什么区别

3 个答案:

答案 0 :(得分:3)

a = 2; // binary 0x10
b = 2; // binary 0x10

c = a + b; // c = 4
c = a | b; // c = 2

|是位操作,不等于+

有时它会给出相同的结果:例如2+12|1;但这不是规则

答案 1 :(得分:2)

|进行按位或运算,即取两个数字中较大的对应位,而+进行两个对应位的相加,并取进一步的进位,表示1+1给出101|1只会停止到1。如果较大的参数在范围内,则|永远不会导致超出范围,而如果两个参数之和导致数字超出范围,则+可能会超出范围。 如果两个数字中的对应位不同,则只有在这种情况下,|才充当+,因为在求和期间,进位永不产生。

答案 2 :(得分:2)

记住|和+是2个不同的运算符。话虽如此,有时它们可​​以具有相同的结果,例如1 * 1和1/1。虽然它们具有相同的结果,但它们不会经过相同的过程。