View previous topic :: View next topic |
Author |
Message |
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu Jan 21, 2021 4:00 pm Post subject: dim Jbit as bit |
|
|
hi
existential question
is there an interest in declaring a bit variable?
does it occupy the place of a byte?
does it slow down the program?
why these questions?
I often use flags to indicate states in my programs, these flags have two states 1 or 0. --- I never use bit, maybe i'm stupid...
JP
(BASCOM-AVR version : 2.0.8.3 ) _________________ pleasure to learn, to teach, to create |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Thu Jan 21, 2021 5:05 pm Post subject: |
|
|
Hi,
in HELP - "Language Fundamentals" the type Bit is described.
If you decleare only one bit, it will occupy a byte. If you declare 8 Bit - still one byte.
Unless you are doings tons of looping calculation with bits, you will probaly not notice a slow down. For setting a bit the related byte has to be read, the bit set and write back again - so is a little more to do.
There is a nasty trap: If you use a bit-var in Mainloop and another bit-var in a interrupt routine - the interupt-bit-var can be altered unintendendly as setting a bit is not an "atomic" statement. |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu Jan 21, 2021 6:28 pm Post subject: |
|
|
thanks laborratte
Yes I read the help, but in my head I can't imagine how the bits are arranged, because they takes place in a byte room,
But As I said it is a philosophical issue, not very important
_________________ pleasure to learn, to teach, to create |
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Thu Jan 21, 2021 6:57 pm Post subject: |
|
|
Hi JP
I create bits like this:
Code: |
'Use with b_GP_Flgs.
'Flags that are not saved.
const c_Serial_Rdy_flg=0
const c_Command_flg=1
const c_Comment_flg=2
'const c_Spare_Flg1=3
'const c_Spare_Flg2=4
'const c_Spare_Flg3=5
'const c_Spare_Flg4=6
'const c_Spare_Flg5=7
dim b_GP_Flgs as byte
.
.
.
if b_GP_Flgs.c_Serial_Rdy_flg = 1 then
'serial data has been received.
b_GP_Flgs.c_Serial_Rdy_flg = 0
.
.
.
|
I measured the speed for using bit like this and if my memory is correct they are very fast like about one or to clock cycles to set/reset bits.
Dave |
|
Back to top |
|
|
Pikczu
Joined: 22 Jan 2006 Posts: 72 Location: Dublin, Ireland
|
Posted: Thu Jan 21, 2021 7:09 pm Post subject: |
|
|
Hi JP
You can always split the byte in to individual bits, just add the dot and individual bit number.
Very useful for all sorts of flags and help bits.
So for example if you declare:
Code: |
Dim flags as byte
flags.0 =1
flags.1 =1
flags.3 =1
flags=0'reset all
'or the same with Bits
flags=bits (0,1,3)
|
Regards
Pawel |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Thu Jan 21, 2021 7:24 pm Post subject: |
|
|
...or in other words
Code: | dim A as Bit
dim B as Bit
dim C as Bit
dim D as Bit
' and optional for Testing:
dim BitContainer as byte at A overlay |
is equivalent to
Code: | dim BitContainer as byte
A alias BitContainer.7
B alias BitContainer.6
C alias BitContainer.5
D alias BitContainer.4 |
|
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Fri Jan 22, 2021 5:08 pm Post subject: |
|
|
hi Laborate, Spiczu, Dave
sometimes it's good to revise the fundamentals
and appreciate your help, the Bascom community is very alive !
I did program from years I never use bit or only in boolean false/true
but indeed the use of Code:
dim A as Bit
dim B as Bit
dim C as Bit
dim D as Bit
' and optional for Testing:
dim BitContainer as byte at A overlay
is equivalent to
Code:
dim BitContainer as byte
A alias BitContainer.7
B alias BitContainer.6
C alias BitContainer.5
D alias BitContainer.4
is very simple
As you said Laborate' in other words' many thanks to Marks to do all this possibilities
bits and Nbits will be very usefull too
JP _________________ pleasure to learn, to teach, to create |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Jan 24, 2021 10:57 am Post subject: |
|
|
If you have enough spare SRAM bytes then use bytes for flags. They are quicker and routines take less flash space.
Sometime ago in someone program for Tiny2313 where was no spare flash place left i shrink the code into 70% flash used only by change all flags on Bits into Bytes.
Checking bytes is easy. Checking Bits means for ex. Byte And &B0000_0001 and then compare 0 or 1. This take time and resources. |
|
Back to top |
|
|
|