考虑以下代码:
x = 1 # 0001
x << 2 # Shift left 2 bits: 0100
# Result: 4
x | 2 # Bitwise OR: 0011
# Result: 3
x & 1 # Bitwise AND: 0001
# Result: 1
Run Code Online (Sandbox Code Playgroud)
我可以理解Python(和其他语言)中的算术运算符,但我从来没有完全理解'按位'运算符.在上面的例子中(来自Python书),我理解左移但不是其他两个.
另外,实际使用的是按位运算符?我很欣赏一些例子.
我想找到一个垂直于给定线的点z(x3,y3).在我的例子中,我给出了2个坐标A(x1,y1)和B(x2,y2).我想找到垂直(AZ)到AB线的点z和距离点B的距离(h).ABZ角是90.这是我的c ++代码.
double AB_slope = m; // know it
//找到垂直于AB线的z点
double AZ_slope = - 1/m;
double x3 = x2 + prescribed_distance * dx;
double y3 = y2 + prescribed_distance * dy;
但我不知道找到dx,dy和prescribed_distance.请帮我.