简体   繁体   English

C#中的按位运算

[英]Bitwise operation in C#

I'm a student working on a project for my college's chemistry department. 我是一名为我大学化学系的项目工作的学生。 Last year I wrote a C++ program to control a picomotor stage and have it communicate with a force pressure sensor, moving the stage up and down until it touched the sensor. 去年,我编写了一个C ++程序来控制微微运动平台,并使其与力压力传感器通信,上下移动平台直至其接触到传感器。 I used the DLL the company provided to write this program. 我使用公司提供的DLL编写此程序。 It took me a while, but I got everything to work. 我花了一段时间,但我却要工作了。

This year, I'm incorporating a second stage to the program and updating the entire program into a GUI using C#. 今年,我将程序的第二阶段纳入其中,并使用C#将整个程序更新为GUI。 The second stage and the force sensor only provide DLLs that work with C#, whereas the old stage was meant for C++. 第二阶段和力传感器仅提供可与C#一起使用的DLL,而旧阶段仅用于C ++。 I was able to get all three to work in the new C# GUI program, it's all almost done save this problem: 我能够在新的C#GUI程序中使所有这三个功能都可以工作,几乎可以解决此问题:

In the old program, I was able to determine if the motors on the stage were moving via this function: 在旧程序中,我可以通过以下功能确定舞台上的电机是否正在运行:

#define MOTOR_MOVING 0x01
byte isMoving = LdcnGetStat(pico_addr) & MOTOR_MOVING;

byte LdcnGetStat(byte address) returns the last status byte of the module at the given address. byte LdcnGetStat(byte address)返回给定地址处模块的最后一个状态字节。 According to the instructional manual I have, bit 0 is suppose to be set when the motor is moving. 根据我的使用手册,假设在电动机运行时将位置0。 So, if I test it in my C++ program, using a bool variable, isMoving is 0 when the motors aren't moving, and 1 when they are, and it promptly returns to 0 after the motors stop moving. 因此,如果我在C ++程序中使用bool变量对其进行测试,则在电机不运动时isMoving为0,在电机不运动时isMoving为1,并且在电机停止运动后立即返回0。 Works fine. 工作正常。

But in C#, I'm having problems incorporating this. 但是在C#中,合并时遇到了问题。

byte isMoving = LdcnGetStat(pico_addr) & MOTOR_MOVING;

doesn't work, I have to do some explicit casting. 不起作用,我必须进行一些显式转换。 So I changed it to this: 所以我将其更改为:

bool isMoving = Convert.ToBoolean(LdcnGetStat(pico_addr) & 
Convert.ToBoolean(MOTOR_MOVING);

That doesn't give me an error, but LdcnGetStat(pico_addr) will always return "true" because it's never 0. If I just test the values that LdcnGetStat() spits out, when the motor is not moving, it returns 28, when it's moving, it return s 98. If I test again post-move, it returns 23. 这不会给我带来错误,但是LdcnGetStat(pico_addr)永远不会返回0,因为它永远不会为0。如果我只是测试LdcnGetStat()吐出的值,则当电动机不移动时,它将返回28,它正在移动,返回98。如果在移动后再次测试,则返回23。

Is there a proper way to deal with this in C#? 在C#中是否有适当的方法来解决这个问题? I'm thinking of checking to see if LdcnGetStat returns the right numerical value, but that doesn't seem like the right way to handle it, especially since the values seem to change post-move. 我正在考虑检查LdcnGetStat是否返回正确的数值,但这似乎不是正确的数值处理方式,尤其是因为该值在移动后似乎发生了变化。

Thanks 谢谢

Conditionals in C/C++ effectively compile down to comparisons against zero. C / C ++中的条件有效地编译为与零的比较。 So the expression LdcnGetStat(pico_addr) & MOTOR_MOVING is equivalent to (LdcnGetStat(pico_addr) & MOTOR_MOVING) != 0 . 因此,表达式LdcnGetStat(pico_addr) & MOTOR_MOVING等效于(LdcnGetStat(pico_addr) & MOTOR_MOVING) != 0 In C# however, conditionals are done with actual bool s, so you can only use the second expression. 但是,在C#中,条件语句是用实际的bool完成的,因此只能使用第二个表达式。

I can explain more if you need me to. 如果您需要我可以解释更多。

I think what you're looking for is: 我认为您正在寻找的是:

bool isMoving = (LdcnGetStat(pico_addr) & MOTOR_MOVING) != 0;

The expression: 表达方式:

byte isMoving = LdcnGetStat(pico_addr) & MOTOR_MOVING;

Probably gives you an error about implicitly converting int to byte . 可能会给您关于将int隐式转换为byte的错误。 You have to cast it: 您必须强制转换:

byte isMoving = (byte)(LdcnGetStat(pico_addr) & MOTOR_MOVING);

In that case, isMoving will be equal to 0x01 when the motor is moving. 在这种情况下,电机移动时, isMoving等于0x01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM