Stuck writing a simple bit toggle routine

New to FreeBASIC? Post your questions here.
dodicat
Posts: 6648
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Stuck writing a simple bit toggle routine

Postby dodicat » Aug 04, 2020 20:52

If you use a macro or define with parameters, and the request is reasonably simple, then it saves writing out overloaded functions.

Code: Select all


#define compare(a_,b_) iif((a_)=(b_),1,0)

print compare("q","q")
print compare(-6675,-6675)
print compare(-66.75,-66.74)
print compare(bit(23,2),-1)

var value=49
print
print compare(7*7,value)+compare(-6675,-6674)+ compare(-66.75,-66.74)+compare("q","q")+compare(bit(23,2),-1)


sleep
 
speedfixer
Posts: 404
Joined: Nov 28, 2012 1:27
Location: California

Re: Stuck writing a simple bit toggle routine

Postby speedfixer » Aug 06, 2020 16:54

Question to all:

I have been using code like this for awhile - but much more complex. It is in a key event thread.

As an example of bit flags, unions, etc - - - anyone see problems with this code?

Code: Select all

' bitflags.bas

type bflags
    union
        allflags as ulong
        type
            f1:1 as boolean
            f2:1 as boolean
            toohot:1 as boolean
            toocold:1 as boolean
            f5:1 as boolean
            fandead:1 as boolean
            f7:1 as boolean
            f8:1 as boolean
            powerzon1:1 as boolean
            powerzon2:1 as boolean
            f11:1 as boolean
            f12:1 as boolean
            f13:1 as boolean
            threadstall:1 as boolean
            f15:1 as boolean
            f16:1 as boolean
            killswitch:1 as boolean
            f18:1 as boolean
            f19:1 as boolean
            f20:1 as boolean
            f21:1 as boolean
            f22:1 as boolean
            f23:1 as boolean
            f24:1 as boolean
            f25:1 as boolean
            f26:1 as boolean
    ' more flags allowed
            cathairball:1 as boolean
            lastflag:1 as boolean
        end type
    end union
end type

dim shared as bflags ptr status

dim as boolean test

sub setupdataholder
' data isolated hare

static as bflags alarms
status = @alarms

status->allflags = 0

end sub

' must instantiate data
setupdataholder

print sizeof(bflags)            ' size of a ulong
print sizeof(status)            ' integer for your system
print sizeof(status->allflags)  ' size of a ulong
print sizeof(test)              ' size of boolean

print status->f1

print " "; status->allflags
print " "; bin(status->allflags, 32)

' set flag
status->toohot = true

print " "; bin(status->allflags, 32)

' toggle flag
status->toohot = not status->toohot

print " "; bin(status->allflags, 32)
print

if status->allflags <> 0 then
    print " *** alarm ***"
else
    print "  all clear"
end if
print

status->cathairball = true

print
if status->allflags <> 0 then
    print " *** alarm ***"
else
    print "  all clear"
end if

print
print " "; bin(status->allflags, 32)
print

sleep


david
Stonemonkey
Posts: 646
Joined: Jun 09, 2005 0:08

Re: Stuck writing a simple bit toggle routine

Postby Stonemonkey » Aug 08, 2020 15:30

Looks like you have other subs for setting and clearing flags too.

Code: Select all

#define tog_flag(flag_,bit_) flag xor=1 shl bit_
#define set_flag(flag_,bit_) flag_ or=1 shl bit_
#define clr_flag(flag_,bit_) flag_ and=&hfffffffe shl bit_
MrSwiss
Posts: 3581
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Stuck writing a simple bit toggle routine

Postby MrSwiss » Aug 08, 2020 15:48

jj2007 wrote:
MrSwiss wrote:Sorry dodicat, the assignment can't be inside the #Define (aka: a function like, single line #Macro).
dodicat's solution works perfectly:
Nobody ever implied, that it doesn't work, but it works as a Sub and not (as intended) as a Function any longer.
The problem here is, that some people just infer things, instead of thinking them through, first.
Thereafter, not quoting the whole context ...

If anyone really wants Sub like behaviour (ByRef return), a #Macro is the way to go.
coderJeff
Site Admin
Posts: 3283
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Stuck writing a simple bit toggle routine

Postby coderJeff » Aug 08, 2020 17:14

MrSwiss wrote:The problem here is, that some people just infer things, instead of thinking them through, first.

Sorry, I could not just let this go by ....

MrSwiss wrote:... it works as a Sub and not (as intended) as a Function any longer.

How do you know what was intended?!?

The fbc sources are full of #define's where an assignment is intended. e.g #define xxxSetyyy( arg ) xxx.yyy = arg
#define / #macro is, at the end of the day, a text expansion and only real difference is interpretation of new lines in the definition.

Return to “Beginners”

Who is online

Users browsing this forum: Google [Bot] and 3 guests