Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

dim Jbit as bit

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 973
Location: France

france.gif
PostPosted: Thu Jan 21, 2021 4:00 pm    Post subject: dim Jbit as bit Reply with quote

hi
existential question Wink

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...
Crying or Very sad

JP

(BASCOM-AVR version : 2.0.8.3 )

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 276
Location: Berlin

germany.gif
PostPosted: Thu Jan 21, 2021 5:05 pm    Post subject: Reply with quote

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
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 973
Location: France

france.gif
PostPosted: Thu Jan 21, 2021 6:28 pm    Post subject: Reply with quote

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
Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
Dave

Bascom Member



Joined: 05 Feb 2005
Posts: 270
Location: McMinnville, OR

usa.gif
PostPosted: Thu Jan 21, 2021 6:57 pm    Post subject: Reply with quote

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
View user's profile
Pikczu

Bascom Member



Joined: 22 Jan 2006
Posts: 67
Location: Dublin, Ireland

ireland.gif
PostPosted: Thu Jan 21, 2021 7:09 pm    Post subject: Reply with quote

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
View user's profile
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 276
Location: Berlin

germany.gif
PostPosted: Thu Jan 21, 2021 7:24 pm    Post subject: Reply with quote

...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
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 973
Location: France

france.gif
PostPosted: Fri Jan 22, 2021 5:08 pm    Post subject: Reply with quote

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 Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 721

poland.gif
PostPosted: Sun Jan 24, 2021 10:57 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum