Society of Robots - Robot Forum
Software => Software => Topic started by: vidam on June 03, 2008, 11:02:48 AM
-
Hi,
I'm converting BASIC to C and would like to know how to convert this simple BASIC statement to C:
Dim TempBit As Bit
I'm using AVR Studio, AVRlib and WinAVR.
I tried this statement but it caused a compiler error in C:
static BIT TempBit;
-Melanie
-
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.
-
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.
Thanks for the tip. And good question. It is being used as a boolean in the code. So I guess I could just convert it to bool in C and be done with it.
BTW, if I do use a char for the other Bits in the basic code, how do you use the bits separately (i.e. what is the syntax)?
-
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:
00000010
If 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:
00000010
Finally to check the second bit we use logic "and" between BitMask and TempBit:
01001010 (TempBit)
&
00000010 (BitMask)
----------------------------
00000010
If the result is greater than zero, the bit is set. If the bit is not set, the result will be zero.
-
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:
00000010
If 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:
00000010
Finally to check the second bit we use logic "and" between BitMask and TempBit:
01001010 (TempBit)
&
00000010 (BitMask)
----------------------------
00000010
If 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?
-
actually you can define bit variables in C
try this
bit Tempbit;
it works with the compiler i use (codevisionAVR)
-
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?
The char datatype is an integer that is a single byte in size. It is used to represent characters, which are just numbers (look up ASCII code). It is probably the smallest datatype you can use on an AVR since registers and RAM locations are all 8-bits in size.
- Ben
-
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
}booleans
access using:
booleans.boolean1 = 1;
or
booleans.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)
-
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
}booleans
access using:
booleans.boolean1 = 1;
or
booleans.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)
Thanks this is really neat. It eliminates the need to do bit-masking.
-
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.html (http://andrey.thedotcommune.com/bitmasking.html)
dunk.
-
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.html (http://andrey.thedotcommune.com/bitmasking.html)
dunk.
The article on bit-masking is just what I needed. Thank you dunk. :-)