Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
I've never used (don't know if they exist even) bit variable types in C. What is the use of TempBit? My suggestion is:char TempBit;A "char" is 8 bits (one byte) long. You can then use all the bits separately.
Well, if you want to use a certain bit you can use a mask. For example to set the second bit, set the char variable to 2:char TempBit = 2;This is how TempBit now looks in binary representation:00000010If we want to check if the second bit is set we can use a bit mask variable. Let's assume some of the other bits have been set in TempBit, by for example an input port (binary rep. TempBit):01001010(73 if you are looking at the decimal representation)Ok, now we create:char BitMask = 2;in binary, BitMask:00000010Finally to check the second bit we use logic "and" between BitMask and TempBit: 01001010 (TempBit)& 00000010 (BitMask)---------------------------- 00000010If the result is greater than zero, the bit is set. If the bit is not set, the result will be zero.
Forgive my ignorance, but I thought char in C could only be a character data type such as the alphabet A-Z. You set a char TempBit = '2'. Confused... How does the compiler interpret a char set to '2' as a number or as a character?
Some compilers allow booleans or single bit. But i think it ends up wasting the other 7 bits in the byte.If you are definately going to use more than 1 boolean it might be worth making a struct of single bit datatypes eg.struct{ char boolean1:1; //set 1 bit to boolean1 char boolean2:1; char boolean3:1; char boolean4:1; char 4bits:4; //set 4 bits to 4bits}booleansaccess using:booleans.boolean1 = 1;orbooleans.4bits = 0b0101;Its just a way to utilise all of the available bits. If you just need 1 boolean, than maybe there is something else that you can get away with using the remaining 7 bits on. (the different allocations also overflow independantly too)
hey Vidam,this has been posted before but it's still useful.if you want a nice rundown on bit-masking check this article: http://andrey.thedotcommune.com/bitmasking.htmldunk.