问题标签 [binary-operators]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
40 浏览

javascript - 使用运算符时,使用多个运算符会影响代码速度吗?

刚刚在课堂上进行过讨论,似乎没有人认为这有什么不同,但我的问题是,如果我使用 1 个二元运算符进行检查,它的执行速度是否比 2 快?很难解释,但这里有一个例子:

我的逻辑告诉我,是的,它确实需要更长的时间。凭借我们强大的系统,我确信它会产生不可衡量的差异,但理论上如果我们将十亿个这样的语句叠加在一起,执行 1 个检查的语句会比另一个运行得更快吗?

0 投票
1 回答
138 浏览

python - Python:切换布尔“一次”的简洁方法(即,在一个方向上)

我发现自己需要在第一次(并且只有那时)发生某些事情时更新一个布尔变量。使用var = not var是不可能的,因为它每次都会继续翻转。

(对不起这个愚蠢的例子;我正在努力寻找一个更明智的例子……)

假设我想尽可能少地“接触”变量——否则我可以通过简单地省略最里面的语句来一次又一次地覆盖它if

基本上,我正在寻找一种更简洁、pythonic 的方式来模拟(三元条件运算符的二进制版本,例如)猫王运算符 ( ?:) 或空合并运算符(例如??; 因语言而异)。

关于如何保持简短(呃)和清晰的任何想法?

0 投票
1 回答
404 浏览

java - 找不到如何为 binaryOperator 接口 java 实现 apply 方法

我需要使用 binaryOperator 实现一个应用方法来执行两个双精度数的数学过程,但不知道如何去做。我的代码的目的是一次对两个数字应用应用方法,每个数字都来自它各自的迭代器。

我编程时间不长,所以我的代码可能有很多错误,但这是我到目前为止的进展:

0 投票
1 回答
197 浏览

c - 在 C 中使用按位操作篡改文件内容

我有这个小程序,它从 allomany.txt读取一个长文本文档。它包含一个长文本,我们有一些数字。现在我需要伪造每个数字:假设程序需要从文件中读取文本,我需要找到一个数字(数字是字符串)。然后我需要检查字符串是否是数字。如果是,那么我需要使用按位运算来伪造它。

伪造:如果我在 string(atoi,sscanf) 中找到一个数字,那么我需要增加在字符串中找到的数字。示例:如果程序找到 14,我们使用按位运算将其增加 15。如果我没记错的话,我在网上找到了一个例子:(-(~n)). 如果我不使用按位运算,它可以工作。

问题:如何使用按位运算来做到这一点?

0 投票
1 回答
667 浏览

java - 按位或表示布尔数组

首先,我是个新手,对按位操作人员的了解很差,如果我的问题看起来很愚蠢,很抱歉。

所以我的 Android 应用中有 5 个复选框,这些复选框的状态需要与后端同步。但是,我得到一个整数(例如 13),而不是一个简单的 JSON 对象,它有 5 个具有真/假值的元素,并且需要发送一个整数来表示所有复选框的状态。API 文档说明如下:

FromUri Int32 可见性:可见性 = 以下值的位“或”运算 None = 0,NameVisibile = 1 | 网络可见 = 2 | 移动可见 = 4 | DOBVisible = 8 | 地址可见 = 16

所以我想这意味着如果我有例如 checkWebVisible和checkboxes 我应该做MobileVisible并发送数字 14 来代表我的复选框的状态?DOBVisible2 + 4 + 8 = 14

如果我从 BE 得到一些整数,我应该用同样的逻辑来解释它吗?

这是解决这个问题的正确方法吗?可能有更多的“智能”方法来使用位运算符来做同样的事情吗?

0 投票
3 回答
3586 浏览

java - 计算二进制整数到0的汉明距离的简单快速方法?

我正在写一个数独求解器,我必须计算我学到的被称为intto0的汉明距离,例如7111二进制) to的汉明0距离3。所以我只是这样做:

虽然效果很好,但我觉得它有点笨拙。我试图想出一个二进制运算技巧来计算距离(主要是为了好玩),但我只能找到一种适用于距离的方法1

我查看了 StackOverflow 和网络,但找不到任何东西。

假设它num永远不是负数并且总是小于512,是否有更好/更优雅的方式来评估它,也许是一些二元运算技巧?如果不是,鉴于上述假设,汉明距离的近似值是否总是在误差范围内< 1

0 投票
1 回答
719 浏览

android - android上的按位运算符:为什么大数不等于

假设我在一个在模拟器(marshmallow,OSX 64 位)上运行的 android 应用程序中编写了一些非常简单的测试,其中 logcat 输出:

…logcat 打印:

显然,最后一行是不正确的:它应该与第三行相同。

为什么 ?我怎样才能让它按预期工作?

0 投票
1 回答
65 浏览

c - While loop iterates itself more or less than it should

Anwers of Austin Hastings and Michael Burr has solved my problem, this loop is always true as I just realised and it's solved when did it like Austin Hastings did. Cannot mark as answer since it is a comment. Thanks for your help!

I have an assignment in C which is to print only specific kinds of numbers between an 128 and 255 (so 8 digit binary representations) and I should do it without using any arithmetic operators. The conditions are:

  1. Number's binary representation has to have same number of 0's and 1's.
  2. Number's binary representation cannot have more 0's than 1's at any moment when reading from left-to-right. For example, 156 (1001 1100) does not meet the 2nd condition because there are two 0's and only one 1 at the 3rd digit, while 210 (1101 0010) satisfies these conditions.

I am using functional implementation and used a single function for these two conditions and that part of code is:

I had wrong output constantly so put some control mechanisms which do not effect the code to track the problem. These are:

  • ct : Counts how many times it has enter into if or else in while loop.

  • printf(" CARRY %d \n", carry) : shows carry's value after it has been decreased one.

  • printf(" breakCounterZero = &d breakCounterOne = %d ") : shows 0 and 1 counts if it has stuck at "if (counterZero > counterOne)" which checks 0 count cannot be higher than 1 count at the end of each while loop.

  • printf("successCounterZero = %d successCounterOne = %d") : shows if it has passed the while loop.

My problem is if I try to let while loop work until carry is used last with carry = 0, it gives output of 210, which should work as it loops 8 times and at CARRY = -1 it should break from the loop while it is like:

so it loops 1 more time than it should and it makes count one more 0 so it fails. But wen I increased limit of carry to 1 in while loop to see what happens, it gives:

so it passes the 3rd condition but one less 0 than it should so fails at 2nd condition.

This seems to be a complicated and a too-specific question, but thanks for any hints.

0 投票
3 回答
1308 浏览

java - Java中字节移位的奇怪行为

当我在 上使用位移时byte,我注意到在使用无符号右移 ( >>>) 时我得到了奇怪的结果。使用int时,右移(有符号:>>和无符号>>>:)都按预期运行:

但是当我对 做同样的byte事情时,无符号右移会发生奇怪的事情:

我认为可能是编译器在内部将其转换byteint,但似乎不足以解释这种行为。

为什么移位在 Java 中以字节的方式表现?

0 投票
2 回答
500 浏览

swift - 快速二元运算符不能应用于操作数

我乞求快速学习,我在 appStore 上以 5 美元的价格购买了一个名为 CODESWIFT 的应用程序。我认为这是从语言开始,熟悉命名事物的新方法等等的一种很好的简单方法......在其中一个练习中,应用程序让您创建这几个变量并将它们组合起来打印到控制台:

当我在应用程序上执行此操作时,它可以工作(应用程序显然无法编译,但它会检查您的代码),但我决定在 Xcode 上运行相同的练习,但它返回错误:

这似乎完全合乎逻辑,但我想我没想到该应用程序会成为骗局。我被抢了5美元吗?