• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

what is the equivalent to PB BIT function?

Started by Chris Chancellor, December 12, 2018, 07:16:00 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

in PB there is a BIT function which is describe as below

what is O2 equivalent to this BIT function ?


BIT function

Purpose
Return the value of a particular bit in an integral class variable (or in an implied bit-array)
Syntax
flag = BIT(intvar, bitnumber)
Remarks
The BIT function is used to determine the value of one particular bit in an integral-class variable or
implied bit-array.

intvar
The parameter intvar must be a variable, not an expression. The BIT function returns
either 0 or 1 to indicate the value of the specified bit.

bitnumber
The bit in question.The allowable range for the parameter is the same as that of a Long-integer.
This makes it possible to have implicit bit-arrays of more than 2 billion bits in size.
For such arrays, bits 0 to 15 are in the first word starting at intvar, bits 16-31 are in the next word, and so forth.

Implied bit-arrays are considered to start at the memory position of the variable intvar. For example, if
intvar is itself an array variable, it is possible to access bits in any of the following elements of the array.
See the array examples below.

Care must be exercised to ensure that the bit index number (bitnumber) does not exceed the
number of bits that can be validly accessed. For example, reading the 17th bit of a 16-bit scalar variable may
trigger a General Protection Fault (GPF). Similarly, adjusting the 4097th bit of a bit-array derived from a
128-element DWORD array may cause similar problems. bitnumber is always zero-based, so the 129th bit of
an implied bit-array is referenced in the BIT statement with bitnumber equal to 128.

For example: x& = BIT(A?(1), 128).

The first bit is the least-significant bit, which is bit number zero. For example:


x% = 7
y% = BIT(x%, 2)
[statements]
DIM z%(1:2000000)  ' 32 million element bit-array
y% = BIT(z%(1),16) ' bit 0 of 2nd word of z%()
y% = BIT(z%(2000000),15)   ' MSB of last element
y% = BIT(z%(1), 31999999&) ' MSB of last element


Charles Pegge

Chris,

This gives most of the functionality:


function bit(sys v,b) as int
return (v>>b) and 1
end function

'TESTS
int a
a=128
print bit(a,7) '1
a=127
print bit(a,7) '0
a=1
print bit(a,0) '1
[/code

Chris Chancellor