question on testing individual bits using integers
Author Message
question on testing individual bits using integers

Here's an interesting question:

Can you write function, in QBasic, which tests an individual bit
in an integer value and returns false if the bit is clear or
true if the bit is set while using only integers?

For example:

Value% = 256
Bit% = 8
PRINT TestBitFN%(256, 8)

This would produce -1 (TRUE) since bit 8 is set in the value 256.

(Bits are, of course, numbered from 0 to 15 going from right to
left.)

The interesting part is to get the function to work for any of the
six{*filter*} bits while ONLY using integers values throughout the program.

Thu, 08 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:

> Here's an interesting question:

> Can you write function, in QBasic, which tests an individual bit
> in an integer value and returns false if the bit is clear or
> true if the bit is set while using only integers?

> For example:

> Value% = 256
> Bit% = 8
> PRINT TestBitFN%(256, 8)

> This would produce -1 (TRUE) since bit 8 is set in the value 256.

> (Bits are, of course, numbered from 0 to 15 going from right to
> left.)

> The interesting part is to get the function to work for any of the
> six{*filter*} bits while ONLY using integers values throughout the program.

This will do it.
Value% must be in the range -32,768 to 32,767.
Bit% must be in the range 0 to 15.

CONST TRUE% = -1, FALSE% = 0
CLS
DO
INPUT "Enter value :", Value%
INPUT "Enter bit to test :", Bit%
PRINT TestBitFN% (Value%, Bit%)
LOOP

FUNCTION TestBitFN% (v%, b%)

IF v% AND (2 ^ b%) THEN
TestBitFN% = TRUE
ELSE
TestBitFN% = FALSE
END IF

END FUNCTION

--

Bangor, Pa. USA

Thu, 08 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:
> Can you write function, in QBasic, which tests an individual bit
> in an integer value and returns false if the bit is clear or
> true if the bit is set while using only integers?

I think the operator "AND" can be used here.
e.g. A = (hex)1F  and you want to see whether its 0th bit is set :

isSet = A AND &H1

or something like that.

Hope this helps!

________________________________________________________________

HTML, CGI, JavaScript, video games, online message system & more

Thu, 08 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

: Here's an interesting question:

: Can you write function, in QBasic, which tests an individual bit
: in an integer value and returns false if the bit is clear or
: true if the bit is set while using only integers?

: For example:

: Value% = 256
: Bit% = 8
: PRINT TestBitFN%(256, 8)

: This would produce -1 (TRUE) since bit 8 is set in the value 256.

: (Bits are, of course, numbered from 0 to 15 going from right to
: left.)

: The interesting part is to get the function to work for any of the
: six{*filter*} bits while ONLY using integers values throughout the program.

The trick is to use masks. A mask is a word 16 bits long that when
AND'd with the 16 bit number will produce a "1" if the bit is set and
a "0" if it is not set. Here are the 16 masks:

For bit 0   &H0001
1   &H0002
2   &H0004
3   &H0008
4   &H0010
5   &H0020
6   &H0040
7   &h0080
8   &H0100
9   &H0200
10   &H0400
11   &H0800
12   &H1000
13   &H2000
14   &H4000
15   &H8000

So TestBitFN%(numb%, bit%) TestBitFN%(256, 8)might look something like this:

Sub  TestBitFN%(numb%, bit%)
case  0
case  1
.
.
case 15
end select
else
end if
end sub

Woody Pope

Fri, 09 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:

>Here's an interesting question:

>Can you write function, in QBasic, which tests an individual bit
>in an integer value and returns false if the bit is clear or
>true if the bit is set while using only integers?

>For example:

>Value% = 256
>Bit% = 8
>PRINT TestBitFN%(256, 8)

>This would produce -1 (TRUE) since bit 8 is set in the value 256.

>(Bits are, of course, numbered from 0 to 15 going from right to
>left.)

>The interesting part is to get the function to work for any of the
>six{*filter*} bits while ONLY using integers values throughout the program.

It's even more interesting to do it without literals!

QBASIC CODE:

DECLARE FUNCTION testbit% (value%, bit%)

zero% = zero% - zero%
one% = zero% ^ zero%
two% = one% + one%
four% = two% + two%
four{*filter*}% = (four% * four%) - two%
fif{*filter*}% = four{*filter*}% + one%

'*******************************
mloop:
'**************************

INPUT ; "Enter Integer Value"; value%

IF value% = zero% THEN
PRINT
GOTO eoj:
END IF

PRINT TAB(fif{*filter*}% * two%); "in binary is ";

FOR x% = fif{*filter*}% TO zero% STEP -one%

IF testbit(value%, x%) THEN
PRINT USING "#"; a% ^ a%;
ELSE
PRINT USING "#"; a% - a%;
END IF

NEXT x%
PRINT
GOTO mloop:

eoj:

FUNCTION testbit% (value%, bit%)

zero% = zero% - zero%
one% = zero% ^ zero%
two% = one% + one%
four% = two% + two%
four{*filter*}% = (four% * four%) - two%
fif{*filter*}% = four{*filter*}% + one%

IF bit% > four{*filter*}% THEN
IF value% < zero% THEN
testbit = one%
ELSE
testbit = zero%
END IF
ELSE

IF (value% AND (two% ^ bit%)) THEN
testbit = one%
ELSE
testbit = zero%
END IF
END IF

END FUNCTION

Fri, 09 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:

> :Here's an interesting question:
> :
> :Can you write function, in QBasic, which tests an individual bit
> :in an integer value and returns false if the bit is clear or
> :true if the bit is set while using only integers?
>It's even more interesting to do it without literals!
>[QBASIC CODE snipped]

Now that's an interesting piece of code!  Using 0 to the 0 power
to define 1.

I like the part where you use the sign of the integer value to test
bit number fif{*filter*}.  Makes sense, that's the sign bit.

The program could be shortened by changing the last part of the
testbit% function from 5 lines

Quote:
>FUNCTION testbit% (value%, bit%)
>...
>ELSE

>        IF (value% AND (two% ^ bit%)) THEN
>                testbit = one%
>        ELSE
>                testbit = zero%
>        END IF
>END IF
>END FUNCTION

to the (more logically correct) single line

testbit = (value% AND (two% ^ bit%)) = (two% ^ bit%)

Sat, 10 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:
>Here's an interesting question:

>Can you write function, in QBasic, which tests an individual bit
>in an integer value and returns false if the bit is clear or
>true if the bit is set while using only integers?

>For example:

>Value% = 256
>Bit% = 8
>PRINT TestBitFN%(256, 8)

>This would produce -1 (TRUE) since bit 8 is set in the value 256.

>(Bits are, of course, numbered from 0 to 15 going from right to
>left.)

>The interesting part is to get the function to work for any of the
>six{*filter*} bits while ONLY using integers values throughout the program.

'
FUNCTION TBIT%(I%,B%)
'
TST%    = I% AND (2 ^ B%)
TBIT%   = NOT(NOT(TST%))
'
END FUNCTION
'

--
http://www.*-*-*.com/

Sat, 10 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

: : Here's an interesting question:

: : Can you write function, in QBasic, which tests an individual bit
: : in an integer value and returns false if the bit is clear or
: : true if the bit is set while using only integers?

: : For example:

: : Value% = 256
: : Bit% = 8
: : PRINT TestBitFN%(256, 8)

: : This would produce -1 (TRUE) since bit 8 is set in the value 256.

: : (Bits are, of course, numbered from 0 to 15 going from right to
: : left.)

: : The interesting part is to get the function to work for any of the
: : six{*filter*} bits while ONLY using integers values throughout the program.

: The trick is to use masks. A mask is a word 16 bits long that when
: AND'd with the 16 bit number will produce a "1" if the bit is set and
: a "0" if it is not set. Here are the 16 masks:

: For bit 0   &H0001
:         1   &H0002
:         2   &H0004
:         3   &H0008
:         4   &H0010
:         5   &H0020
:         6   &H0040
:         7   &h0080
:         8   &H0100
:         9   &H0200
:        10   &H0400
:        11   &H0800
:        12   &H1000
:        13   &H2000
:        14   &H4000
:        15   &H8000

: So TestBitFN%(numb%, bit%) TestBitFN%(256, 8)might look something like this:

:
: Sub  TestBitFN%(numb%, bit%)
:           case  0
:           case  1
:               .
:               .
:           case 15
:    end select
:    if answer% > 0 then
:    else
:    end if
: end sub

: Woody Pope

CORRECTION:

select case bit% and not mask%.
Sorry..

Woody

Sat, 10 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:

>Now that's an interesting piece of code!  Using 0 to the 0 power
>to define 1.

>I like the part where you use the sign of the integer value to test
>bit number fif{*filter*}.  Makes sense, that's the sign bit.

>The program could be shortened by changing the last part of the
>testbit% function from 5 lines

>>FUNCTION testbit% (value%, bit%)

>>...

>>ELSE

>>        IF (value% AND (two% ^ bit%)) THEN
>>                testbit = one%
>>        ELSE
>>                testbit = zero%
>>        END IF
>>END IF

>>END FUNCTION

>to the (more logically correct) single line

>         testbit = (value% AND (two% ^ bit%)) = (two% ^ bit%)

Glad you liked 0^0=1, but disagree on shorting the code. 2^15 =32768 is not
a valid QBASIC integer. You have to test for bit% > 14.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|Richard S. Emerson    | Walk softly, but carry a |

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sat, 10 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:

> :Can you write a function, in QBasic, which tests an individual
> :bit in an integer value and return false if the bit is clear or
> :true if the bit is set while using only integers?
>This will do it.
>CONST TRUE% = -1, FALSE% = 0
>CLS
>DO
>   INPUT "Enter value :", Value%
>   INPUT "Enter bit to test :", Bit%
>   PRINT TestBitFN% (Value%, Bit%)
>LOOP
>FUNCTION TestBitFN% (v%, b%)
>IF v% AND (2 ^ b%) THEN
>   TestBitFN% = TRUE
>ELSE
>   TestBitFN% = FALSE
>END IF
>END FUNCTION

This seems to be the most straight-forward way.

My original question should have allowed a long integer for
the value to be tested so that all six{*filter*} bits might
be tested easily (without having to calculate the negative
version of numbers greater than 32767).

An interesting change to the above would be to reduce the
Function to a single line which has the true/false logic
built-in.

That is:

CLS
DO
'also make value to be tested a long integer
INPUT "Enter value :", Value&
INPUT "Enter bit to test :", Bit%
PRINT TestBitFN% (Value&, Bit%)
LOOP

FUNCTION TestBitFN% (v&, b%)

TestBitFN% = (v& AND (2 ^ b%)) = 2 ^ b%

END FUNCTION

Mon, 12 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

<SNIP>

Quote:
>CLS
>DO
>   'also make value to be tested a long integer
>   INPUT "Enter value :", Value&
>   INPUT "Enter bit to test :", Bit%
>   PRINT TestBitFN% (Value&, Bit%)
>LOOP

FUNCTION TestBitFN% (v&, b%)

TestBitFN% = ( (v& AND (2 ^ b%)) > 0 )
' OR
' TestBitFN% = ( v& AND ( 2^b%) )  ' and leave it at that!

END FUNCTION

____    _    ____      ____  _____
|  _ \  / \  / ___) __ | ___)(_   _) |  Don Schullian
| |_)  / _ \ \____\/  \|  _)   | |   |    Gramou 33
|____//_/ \_\(____/\__/|_|     |_|   |  Papagou, 15669
___________________________________  |      Greece
=================================== |  ++30-1-654-6200

Nutz 'n Boltz for powerbasic v3.2
A library of everyday goodies

Mon, 12 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Quote:

>FUNCTION TestBitFN% (v&, b%)

>  TestBitFN% = ( (v& AND (2 ^ b%)) > 0 )
>' OR
>' TestBitFN% = ( v& AND ( 2^b%) )  ' and leave it at that!

>END FUNCTION
>TestBitFN% = ( (v& AND (2 ^ b%)) > 0 )

This first version is even simpler yet.

Quote:
>TestBitFN% = ( v& AND ( 2^b%) )

This second version, however, does not return the correct
true / false condition.

Mon, 12 Jul 1999 03:00:00 GMT
question on testing individual bits using integers

Hi Don,

How's the bread...???? SInce testbit is the topic I give away
prematuraly a snipp from a contribution of mine for next ABC....

DEF SEG = VARSEG(bits\$): offcode% = SADD(bits\$)

'1) Let us determine functionality
FOR i% = 0 TO 31
value& = &H1234FDEC: bits% = i%
CALL absolute(value&, bits%, offcode%)
PRINT bits%;
NEXT
PRINT

FOR b% = 0 TO 30
PRINT ((value& AND (2 ^ b%)) > 0);
NEXT

'First point is that the formula caused overflow for bit 31
'( although thats easy enough to encounter)
'Second point is that it returns -1 for set bits( I like 1 better)
'Third: the speed

a! = TIMER
FOR j% = 0 TO 1000
FOR i% = 0 TO 31
bits% = i%
CALL absolute(value&, bits%, offcode%)
NEXT
NEXT
b! = TIMER

c! = TIMER
FOR i% = 0 TO 1000
FOR b% = 0 TO 30
res% = ((value& AND (2 ^ b%)) > 0)
NEXT
NEXT
d! = TIMER
PRINT : PRINT :
COLOR 0, 7: PRINT "Bitread": COLOR 7, 0
PRINT "Assembly use :  "; b! - a!
PRINT "^ and AND use:"; d! - c!
PRINT : COLOR 0, 7:

END

'------------------------------------------------------------------
'Try to beat the readbit functions
'For assembly programmers like Tyler
'and Daniel: I used a trick to jump only
'---------------------------------------------------------------------
asm\$ = asm\$ + CHR\$(&H55)                           'push bp
asm\$ = asm\$ + CHR\$(&H89) + CHR\$(&HE5)              'mov bp,sp

'Get bit to read into CL
asm\$ = asm\$ + CHR\$(&H8B) + CHR\$(&H5E) + CHR\$(&H6)  'mov bx,[bp+06]
asm\$ = asm\$ + CHR\$(&H8B) + CHR\$(&HF)               'mov cx,[bx]
'Get word to check into ax
asm\$ = asm\$ + CHR\$(&H8B) + CHR\$(&H7E) + CHR\$(&H8)  'mov di,[bp+08]
asm\$ = asm\$ + CHR\$(&H8B) + CHR\$(&H5)         'mov ax,[di]  get lowword
asm\$ = asm\$ + CHR\$(&H80) + CHR\$(&HF9) + CHR\$(&H10) 'cmp cx,16
asm\$ = asm\$ + CHR\$(&H72) + CHR\$(&H6)               'jb go to check
'Check highword
asm\$ = asm\$ + CHR\$(&H8B) + CHR\$(&H45) + CHR\$(2)    'mov ax,[di+2]get
highword
asm\$ = asm\$ + CHR\$(&H83) + CHR\$(&HE9) + CHR\$(&H10) 'sub cx,16
asm\$ = asm\$ + CHR\$(&HD3) + CHR\$(&HE8)              'shR ax,cl
asm\$ = asm\$ + CHR\$(&H25) + CHR\$(&H1) + CHR\$(&H0)   'and ax,1  return 1
for set
'Return the bit
asm\$ = asm\$ + CHR\$(&H89) + CHR\$(&H7)               'mov [bx],ax

asm\$ = asm\$ + CHR\$(&H5D)                           'pop bp
asm\$ = asm\$ + CHR\$(&HCA) + CHR\$(&H4) + CHR\$(&H0)   'retf 4

END FUNCTION

Quote:

><SNIP>
>>CLS
>>DO
>>   'also make value to be tested a long integer
>>   INPUT "Enter value :", Value&
>>   INPUT "Enter bit to test :", Bit%
>>   PRINT TestBitFN% (Value&, Bit%)
>>LOOP

>FUNCTION TestBitFN% (v&, b%)
>  TestBitFN% = ( (v& AND (2 ^ b%)) > 0 )
>' OR
>' TestBitFN% = ( v& AND ( 2^b%) )  ' and leave it at that!
>END FUNCTION

Tue, 13 Jul 1999 03:00:00 GMT

 Page 1 of 1 [ 13 post ]

Relevant Pages