Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bitset with varbinary data

Status
Not open for further replies.

markftwain

Technical User
Jul 12, 2006
108
Hi,

I have serveral hundred true-false answers I wish to store as a single varbinary field or memory string. I would like to set or clear a given bit, by bitset(varbinary,nbit) or bitclear (varbinary,nbit). So far no luck, any ideas?

I'm using vfp 9 sp2.
varbinary is of q(254).
0 < nbit < 200

Thanks
 
Theres got to be a better way then this:

x = [0h]+replicate(chr(0), ceiling( maxflag/32)*4))

procedure setvarbinary ( x as varbinary @, nbit as bitnumber )
n4byte = int(nbit/32)
n32bit = mod(nbit,32)
z = substr(x,3+4*m.n4byte,4)
num = ctobin(z,"4rs")

num = bitset(num,n32bit)

y= bintoc(num,"4rs")
x= stuff( x,3+4*m.n4byte,4,y)
endproc

procedure clearvarbinary ( x as varbinary @, nbit as bitnumber)
n4byte = int(nbit/32)
n32bit = mod(nbit,32)
z = substr(x,3+4*m.n4byte,4)
num = ctobin(z,"4rs")

num = bitclear(num,n32bit)

y= bintoc(num,"4rs")
x= stuff( x,3+4*m.n4byte,4,y)
endproc

Any ideas?
 
Mark,

I can't quite figure out what your code is trying to do.

If you have a varbinary value, Q, and you want to set bit N to 1, why not just:

BITSET(Q, N)

Similarly, to set bit N to zero:

BITCLEAR(Q, N)

Am I missing something?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Bitset and bitclear work, maybe you just did BitSet(Varbinary,nBit) instead of REPLACE varbinary WITH BitSet(varbinary,nBit).

BitSet does not treat the first parameter as passed in by reference, it returns the changed varbinary as it's result, eg

Code:
Local lqTest

lqTest = CreateBinary(Replicate(Chr(0),20))
?lqTest
Bitset(lqTest,0)
?lqTest
lqTest = Bitset(lqTest,0)
?lqTest

It's just the right usage of Bittest. If you have problems with a function, please refer to the help file.... and don't stop reading after the first sentence...

Bye, Olaf.
 
Bitset(), bittest(), bitclear() work on a varbinary() but you cannot use bintoc(),ctobin() with varbinary().

Code:
Local ix,jx,v
Rand(-1)

Create Cursor xx (Id i, bitval Varbinary(254))
For ix = 1 To 10
  * set max 5 random bits and insert
  v = Createbinary(Replicate(Chr(0),254))
  For jx = 1 To 5
    v = Bitset(m.v, Int(Rand()*(254*8-1)))
  Endfor
  Insert Into xx Values (m.ix, m.v )
Endfor

* read back set bits
Clear
Scan
  ? Recno()
  For ix = 0 To 254*8-1
    If Bittest( xx.bitval, m.ix )
      ?? m.ix
    Endif
  Endfor
Endscan

Cetin Basoz
MS Foxpro MVP, MCP
 
Thanks everybody.

I was doing this:
x = [0h]+replicate(chr(0),100)
? bitset(x,65)

Error: "Function argument, value, type, or count is invalid".

I thought x was a valid varbinary. I was wrong. Same code with createbinary() works great!

Thanks again. Problem solved.

(My code was to isolate a 4 byte integer in which bitset() always worked).
 
eval(x) would be a varbinary, if it contains the string beginning with [0h], as you can also write a varbinary value in the syntax 0habcdef123 etc. But what you created there is a string in x, not a varbinary. Createbinary(x) would also not be what you think, eg try ? createbinary([0h0]). you need cratebinary(replicate(chr(0),N) with N being the number of bits you want divided by 8, as each chr(0) is for 8 bits. So if you need 200 bits, you nbit needs to run from 0 to 199 and you need 25 bytes for this, so a Q(25) field is sufficient.

Eg for 8 bits = 1 byte do ? createbinary(chr(0)), that will print 0h00.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top