Determine Bit Value in Byte

New to FreeBASIC? Post your questions here.
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Determine Bit Value in Byte

Post by mark bower »

is there a simpler, maybe even faster, way to detect the value of a bit in a return of byte data versus code below which does the job? the presence or absence of the bit tells whether an interupt has been generated.

Code: Select all

'file: bit_value
'DOES: detect value of 5th bit in data byte

dim as integer a    '114
dim as string b     'binary of a = 01110010

'note: if the 5th bit in b (bits run 0 to 7) is set to 1, INT returned

a = 114
b= bin(a)

If Mid( Right (b,5),1,1)= "1" then 
print "interrupt returned"
else
    print "no interrupt"
end if

sleep:end
mark
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

take a look at the 'bit' keyword.

Code: Select all

b=bit(a,5)
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

that definitely nails it - sorry after some searching i did not find on my own.
tx - mark
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Code: Select all

Const as Integer One = 1, Two = 2, Three = 4, Four = 8, Five = &h10, Six = &h20 '' etc...

Dim as integer MyInt
'' Set a bit
MyInt or = Four
Print bin(MyInt)

'' Flip a Bit
MyInt xor = Four
Print bin(MyInt)

'' Flip again
MyInt xor = Four
Print bin(MyInt)

'' Set a few more bits
MyInt or = One
MyInt or = Six
Print bin(MyInt)


'' Unset a bit.
MyInt and = not Four
Print bin(MyInt)

sleep
Lol :D
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

@anonymous1337
i have next to no understanding of the boolean operators, so will have to play with your code a little.

tx - mark
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

o.k. - i do not get even the first example where MyInt & Four are "or ="(or'd and assigned). the wiki example i understand where two numbers are or'd, but the example starts out with MyInt = 0, does it not? it would seem to me that a "0" or = 4 would still be 4, not 8(bit 4 set to 1)?

Code: Select all

Dim As Integer MyInt
'' Set a bit
MyInt Or = Four
Print bin(MyInt)
mark
Mlok
Posts: 123
Joined: Mar 08, 2006 1:07
Location: Czech Republic
Contact:

Post by Mlok »

You may want to check the wiki for SHL and SHR functions as well. [Yoda]Useful can they be.[/Yoda]
Though for your program, BIT is definitely the choice.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

@mark

I don't know why anonymous1337 is trying to show you how to set bits.. Lol?

phishguy's method is correct, but from your OP it appears that you want bit #4 ("the 5th bit" from the right), where bits are numbered right to left from zero. So bit(a,4).

Code: Select all

'
'   7   6   5   4   3   2  1  0 ' bit "place" number
'   0   1   1   1   0   0  1  0 ' 114, binary
                ^               ' the "5th bit", bit #4 
' 128  64  32  16   8   4  2  1 ' decimal value of each bit-place
' 
'
'bit(a,4)
Note that the decimal value of bit #4 is 16. If you use the binary operator "and", as in:

Code: Select all

'
print 114 and 16
'
'   0   1   1   1   0   0  1  0 ' 114, binary
'   0   0   0   1   0   0  0  0 '  16, binary
'
the return value will be 16 if bit #4 is set in 114, 0 otherwise.

Your code with all 3 methods:

Code: Select all

'file: bit_value
'DOES: detect value of 5th bit in data byte

dim as integer a    '114
dim as string b     'binary of a = 01110010

'note: if the 5th bit in b (bits run 0 to 7) is set to 1, int returned

a = 114
b= bin(a)
'
if mid( right (b,5),1,1)= "1" then 
    print "interrupt returned"
else
    print "no interrupt"
end if
'
if (bit(a,4)) then ' bit(a,4) = -1
    print "interrupt returned"
else
    print "no interrupt"
end if
'
if (a and 16) then ' a and 16 > 0
    print "interrupt returned"
else
    print "no interrupt"
end if
'
sleep:end
I'm not always the best choice to explain things..
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Zippy wrote:I don't know why anonymous1337 is trying to show you how to set bits.. Lol?
'note: if the 5th bit in b (bits run 0 to 7) is set to 1, INT returned
If ( MyBit AND Value ) then "Interuapt ratarned!"
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

anonymous1337 wrote:
Zippy wrote:I don't know why anonymous1337 is trying to show you how to set bits.. Lol?
'note: if the 5th bit in b (bits run 0 to 7) is set to 1, INT returned
If ( MyBit AND Value ) then "Interuapt ratarned!"
See:
mark bower wrote: is there a simpler, maybe even faster, way to detect the value of a bit in a return of byte data
<snip>
"detect the value of a bit". Read not set.

See:
mark bower wrote: <snip>
If Mid( Right (b,5),1,1)= "1" Then
Print "interrupt returned"
Else
Print "no interrupt"
End If
<snip>
a unique method of determing the state of a bit. Read not set.

Not to mention that MyInt Xor = Four is pretty obtuse for the OP's level. I didn't say advanced, I said obtuse.
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

@Zippy
i understand all that you presented. i just do not understand what is going on with what anonymous1337 presents, althoh i understand the direction after your piece. bit# X and bit from right is solid terminology to avoid confusing from my perspective.

phishguy's does do the trick perfectly. not to confuse, it turns out that it is bit #7, or 8 bits from right that i need to monitor for end of A/D conversion. thus my code fragment for the wait period to see that A/D conversion is completed now looks like following:

Code: Select all

while bit (Inp(BAD + &h8),7)<> 0   'EOC bit must be 0 to continue, 1 = busy
Wend
i updated my code on post titled Register Program, DAS1601, . . . i am purely delighted with the support and ability to talk to the AD card in the absence of a 32 bit library, and in defiance of the manufacture's engineering non-support.
mark
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

In case it isn't clear yet:

-Xor to toggle a bit (for example Xor with &h1 to toggle the lowest bit)
-Or to set a bit (for example Or with &h1 to turn the lowest bit on)
-And to check a bit (for example And with &h1 will return be nonzero if the lowest bit is set)
-And also to turn off all bits except the ones you want (for example And with &h1 will turn off all bits but the lowest - of course if the lowest is already off the result is 0)
-And Not to turn off only certain bits (for example And with Not &h1 to turn off the lowest bit)

Every programmer should be required to know these things, they are enormously useful and will keep you from using twenty integers when you only need a couple bytes of flags.
sir_mud
Posts: 1402
Joined: Jul 29, 2006 3:00
Location: A van down by the river
Contact:

Post by sir_mud »

Exactly the reason i wrote my BitHolder library, very similiar to the built-in bit commands but allows arbitrarily long or short bitfields.
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

i understand all but the last anonymous1337 code example.

Code: Select all

'' Unset a bit.
MyInt And = Not Four      '1000    four
Print bin(MyInt)          '0111    NOT four
                        '101001    previous val assigned to MyInt
                        '000001    what i get when MyInt AND'd with NOT four ?? 

'the result should be 100001
what might i be doing wrong when i manually calculate 000001 vs code gererated 100001?
mark
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

Not(4) for an integer value (32 bits) is:


FFFFFFFB (hex)
11111111111111111111111111111011 (binary)


four is 0100 (binary)
<edit>
Correction:
I see your using bit position 4 and not the value 4.
so not(8) is:

FFFFFFF7 (hex)
11111111111111111111111111110111 (binary)
101001 ' and initial value
100001 ' result
Post Reply