i am new to c# i have been working on controlling a io card
i have no issue writing to turn on / toggle / or get status

turn on bits |= 1 << bit;
toggle bits ^= 1 << bit;

but i cannot get the item pin to turn off i do not want to use toggle because i want it
to either be on or off

now when i turn off the pin basicly unset the pins i always get a error about it setting a negative number to a ushort
any help would be greatly appreciated

bits &= ~(1 << bit);

//program.cs these are my calls from the main program

                if (whatTheUserTyped == "on")



                if (whatTheUserTyped == "off")

// control.cs 

public string PinOn(int pin1)
bits |= Convert.ToUInt16(1 << pin1);

return "pin on";


public string PinOff(int pin1)

bits &= ~(1 << 3);

return "pin off";
2 Years
Discussion Span
Last Post by HITMANOF44th

There's usually more than that. That is, not only do you set the port value as you noted but if the pin is input, output, etc. Code alone isn't enough here.


a bits is a numerics single value, either 1 or 0 that encodes a single unit of digital information. A byte is a sequence of bits; usually eight bits equal one byte.


Few things here i cant seem to understand in ur code:
-your pinon method takes only 1 argument of type int, but u seem to be passing 5 integer values.
- pinoff method takes a argument and being used at all.

It will be helpful if you could give a intro on ur io card and what exactly u are trying to achieve when u set and unset each bit.


its not the full code since it it long i have overflow meadthods that take more and more but i striped the program to barebones
to not make it a long read i included what i am doing to turn the card pins on and add off i have no issue posting
more of the code also i am working with turning the relays on and off and on the card itself you write to either offset 0 controls 0-7 and offset 4 controls 7-8, the byte itself uint16 or ushort also the code is enough only thing missing
is the WriteToCard(); function and the declared variables of bits to make this work, my main issue that i am having is using the
bitwise to unset the pin


i figured it out

bits & ~(1 << pin)

will actully work when you put it into a static int and return bits & ~(1 << pin)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.