On the good old Arduino, I have a two commands Bitset and Bitclear where I can set or clear any bit in a byte. Which when bytes often relate to ports means I can very quickly switch eight things on or off by just pushing a byte at an 8-bit port. Brilliant when reading a data string into a port to get a pre-educated movement.
Much faster than eight separate pin commands.
But C# in Unity seems woefully short of direct bit controls on a variable. Yes, you can do many boolean operations but none that just set the required bit at the required location.
I am new to scripting c# and despite my multiple linguistic pasts seem to not be able to lift the veil on what should be a simple task.
I can’t find it, but I suspect audrino’s bitset isn’t real. It’s probably a function for beginners which uses the real bit operations. Auduino uses C++, right? C# copied C++'s bit operations, and so did everyone else, so any reference to any bit operations should be fine.
To clarify my example above, this is what’s happening:
myByte is just 0: or 00000000 in binary (technically it’s a 32 bit int but let’s ignore the most significant 24 bits)
We picked “3” as the bit number
1 in binary is 00000001
When we do “1 << 3” that means “shift the bits of 1 left 3 spaces”. The result is 00001000
Then we are using the ‘|’ operator which is a bitwise OR. That means do a logical OR operation between each corresponding bit of the two operands. In our case the operands are 00000000 and 00001000.
The result of 00000000 | 00001000 is 00001000.
So in effect we have set a single bit in our original number, which was 0. You might say “hey that’s not the third bit, that’s the fourth bit” to which I would probably agree with you .
Yes but if there port was set to 65 prior to your code pushing the bits around like that makes the robot smack the operator in the mouth. LOL. Thats why I need to be able to just change the bit without affecting any others that are already set. Its like a pin command. but on the variable. Pin High Pin Low, Bit High Bit Low.
Let me explain, the variable / byte already has a value and shushing the bits around will cause the robot to smack the operator in the mouth. Just lin a PIn high Pin low, I need to make a Bit high or low without affecting the rest of the variable. Each bit will in three more commands refer to a port control.
If you want to UNSET the 3rd bit (or any bit) you can do this:Valves = Valves & ~(1 << 3)
So what that does is create the shifted 1 bit again:
00001000
Then the ~ inverts that:
11110111
Then using & does a bitwise-AND between 11110111 and whatever Valves is.
you can also shorten these a bit like this:
// set a bit
Valves |= 1 << 3;
// unset a bit:
Valves &= ~(1 << 3);
This line makes no sense and you have to be careful with the order of operations. Have a look at this documentation.
The unary “not” operator has the highest order of all your used operators. That means the first thing your code does is turn the “0” into an “0xFFFFFFFF”. The second in command is the bit shift to the left by 4 places. So you turn your 0xFFFFFFFF into 0xFFFFFFF0. Next one will be the bitwise and operator which of course would clear out the lowest 4 bits.
When you look at PraetorBlue’s code he has brackets around the (1<<4) expression. So we first get 0x00000010. Next we invert the result so instead of having a 1 at the bit position we are interested in, we have everywhere a 1 except the bit we’re interested in: 0xFFFFFFEF. Now the “and” operation will “filter out” the 4th bit.
ps: The Arduino board is not a microcontroller but the board itself. The classic arduino had an Atmel AVR µC while newer ones seem to use an ARM chip and some other types. Those microcontrollers are all RISC chips. Those usually have dedicated opcodes for setting / clearing bits of a register. Most actual CPUs do not have such opcodes.
I never used the Arduino board as it was too expensive for what it offered ^^. I mainly used PIC microcontrollers. Depending on the features you could get them for around 1€ / $1 about 15 years ago. I used them before Arduino was even a thing. The great thing about most PICs was they usually had an internal RC clock so you didn’t need an external clock. Allmost all chips came in a C(EPROM) and an F(Flash) variant. The C variant was mainly for mass production and was a lot cheaper. The smallest PIC I have is a 6 pin chip ^^ It has two pins for power and 4 I/O pins, incredible small. Another great thing was, if you don’t need extreme fast speeds they didn’t require much power. They also had a sleep mode. It could wake itself through a timer or through an interrupt. This allowed battery driven applications that could run for ages. Haven’t done much with µC for years now. Btw: PICs were more RISCy than the AVR chips. The instruction set usually fits on one page ^^.
I too started with PIc’s in the naughties when I taught electronics. But I went over to PicAxe chips because of the built in Bootstrap which allowed my pupils to program in circuit. A much better option in my opinion as pupils are pants at getting chips out of sockets often breaking legs and need hundreds of program itterations as they learn. I had 10 PC’s in my room with programming leads so 2 to 3 pupils per project. Tried stamps but they were to expensive for education. Then I had a love afair with the Paralex Propeller chips, but the Prop2 never manifest itself. So when I found Arduino I went with them, Not the fastest not the best but lots of backing, I now build my own circuits with embedded controllers. I looked at the Pie last year but found them too quirky and the community is far from helpfull. Ok if you want a gaming box but poor at real machine control. So for now it’s Arduino all the way for me. I have my own PCB prototyping plant so I can experiment at will before sending to a PCB manufacturer. Keeps the costs down. Fortunatley my end product can absorb the costs. Thanks for the reply it is nuch appreciated.