Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Referencing a bit

0 views
Skip to first unread message

Emo1440

unread,
Aug 6, 1997, 3:00:00 AM8/6/97
to

bla bla

R.E.Donais

unread,
Aug 6, 1997, 3:00:00 AM8/6/97
to

emcc...@aol.com (EMcCrosk) wrote:

>
>I am wanting to find out how to reference a single bit in a byte.
>
>Say I have a variable B : byte. How would I find out what the value of
>the third bit (for example) of that variable would be? Also how would I
>set the value of a bit?
>
There are two approaches. You can manipulate the byte
directly, or if using one of the new versions of TP, you can
define the byte as a set.

VAR B: Set of 0..7; { defines a byte as a bitset }

Since bits are numbered from 0 to 7, the 3rd bit is 2, hence
to test if bit 2 is set simply use: If 2 in B Then ...

To set bit 2, use: B := B + [2];
To clear bit 2, use: B := B - [2];

Manipulating the byte directly:

VAR B: Byte;

Its still best to think of the bits as being numbered from 0
to 7. To test if bit 2 is set simply use:
If odd(B shr 2) Then ...

To set bit 2, use: B := B or (1 shl 2);
To clear bit 2, use: B := B and NOT(1 shl 2);

...red

--
Support the anti-Spam amendment
Join at http://www.cauce.org/

Asbjørn

unread,
Aug 7, 1997, 3:00:00 AM8/7/97
to

Mark Wilde wrote:

>
> EMcCrosk wrote:
> >
> > I am wanting to find out how to reference a single bit in a byte.
> >
> > Say I have a variable B : byte. How would I find out what the value of
> > the third bit (for example) of that variable would be? Also how would I
> > set the value of a bit?
> >
> > Thanks in advance

> to test a bit AND the byte with the value of the bit. Bit 3 has the
> value
> of 8. So the code might look like
>
> . . . . . .
> if (b and 8) = 8 then
> writeln ('bit three is set')
> else
> writeln ('bit three is clear');
> . . . . . .

Just one tiny thing :) I think it might be "smarter" to write

if (b and 8) = 0 then
{ bit clear }
else
{ bit set }


Since the outcome should be a "value" / "no value" kinda thing...
i think it would be easier to modify in the future, that's all,
nothing wrong with the original

--
- Asbjørn / Lord Crc

http://home.sn.no/~bheid/
lor...@hotmail.com

Bevyn Douglas Quiding

unread,
Aug 7, 1997, 3:00:00 AM8/7/97
to

In article <19970806181...@ladder02.news.aol.com>,
emcc...@aol.com (EMcCrosk) wrote:

>I am wanting to find out how to reference a single bit in a byte.
>
>Say I have a variable B : byte. How would I find out what the value of
>the third bit (for example) of that variable would be? Also how would I
>set the value of a bit?
>
>Thanks in advance

if bit zero is the least significant bit and bit 7 is the most significant
bit then

c:= B shl bitnum and $fe;

c will equal the value of the bit bitnum in B.

Bevyn.

--
disclaimer

not only did i not post this i have never seen any of these letters before in my life.

Guaranteed silly posts or my money back!!!

Simon Lodal

unread,
Aug 8, 1997, 3:00:00 AM8/8/97
to

Mark Wilde <wi...@provide.net> wrote in article
<33E92D...@provide.net>...

> EMcCrosk wrote:
> >
> > I am wanting to find out how to reference a single bit in a byte.
> >
> So the code might look like
> . . . . . .
> if (b and 8) = 8 then
> writeln ('bit three is set')
> else
> writeln ('bit three is clear');
> . . . . . .
>
>
> Mark Wilde
> Ypsilanti, MI, USA

Better: If (B and 8)<>0 then {true} else {false}. For those of us really
concerned with speed: Due to processor design it's faster to compare
against 0 than against any other number.

Simon


Mark Wilde

unread,
Aug 9, 1997, 3:00:00 AM8/9/97
to


So, You! are one of the Arogant ****** ******* who
insist I reconfigure my system to have 620K free
so the program, that is the only that you do for
8 hours a day, 5 days a week, 52 weeks a year, can
run for five minutes before it blows up???


Sorry unrelated RANT.....

Osmo Ronkanen

unread,
Aug 9, 1997, 3:00:00 AM8/9/97
to

In article <33ec0583....@news.southeast.net>,
R.E.Donais <rdo...@southeast.net> wrote:

>emcc...@aol.com (EMcCrosk) wrote:
>
>>
>>I am wanting to find out how to reference a single bit in a byte.
>>
>>Say I have a variable B : byte. How would I find out what the value of
>>the third bit (for example) of that variable would be? Also how would I
>>set the value of a bit?
>>
>There are two approaches. You can manipulate the byte
>directly, or if using one of the new versions of TP, you can
>define the byte as a set.
>
>VAR B: Set of 0..7; { defines a byte as a bitset }
>
>Since bits are numbered from 0 to 7, the 3rd bit is 2, hence
>to test if bit 2 is set simply use: If 2 in B Then ...
>
>To set bit 2, use: B := B + [2];
>To clear bit 2, use: B := B - [2];
>
>Manipulating the byte directly:
>
>VAR B: Byte;
>
>Its still best to think of the bits as being numbered from 0
>to 7. To test if bit 2 is set simply use:
> If odd(B shr 2) Then ...

Or one could use "if (B and (1 shr 2))>0 then" (or just "if (B and
4)>0" both procuce same code which is better than in your example:

PROGRAM.5: if b and (1 shl 2)>0 then b:=1 else b:=2;
cs:000F A05000 mov al,[PROGRAM.B]
cs:0012 2404 and al,04
cs:0014 08C0 or al,al
cs:0016 7607 jbe 001F

cs:0018 C606500001 mov byte ptr [PROGRAM.B],01
cs:001D EB05 jmp PROGRAM.6 (0024)
cs:001F C606500002 mov byte ptr [PROGRAM.B],02

PROGRAM.6: if odd(b shr 2) then b:=1 else b:=2;
cs:0024 A05000 mov al,[PROGRAM.B]
cs:0027 30E4 xor ah,ah
cs:0029 C1E802 shr ax,02
cs:002C D0E8 shr al,1
cs:002E 7307 jnb 0037

cs:0030 C606500001 mov byte ptr [PROGRAM.B],01
cs:0035 EB05 jmp PROGRAM.7 (003C)
cs:0037 C606500002 mov byte ptr [PROGRAM.B],02


Note that using sets produces most compact comparison code:

PROGRAM.6: if 2 in bs then bs:=bs+[3] else bs:=bs-[3];
cs:000F F606510004 test byte ptr [PROGRAM.BS],04
cs:0014 740A je 0020
cs:0016 A05100 mov al,[PROGRAM.BS]
cs:0019 0C08 or al,08
cs:001B A25100 mov [PROGRAM.BS],al
cs:001E EB08 jmp PROGRAM.8 (0028)
cs:0020 A05100 mov al,[PROGRAM.BS]
cs:0023 24F7 and al,F7
cs:0025 A25100 mov [PROGRAM.BS],al

>
>To set bit 2, use: B := B or (1 shl 2);
>To clear bit 2, use: B := B and NOT(1 shl 2);
>
> ...red
>
>--
>Support the anti-Spam amendment
> Join at http://www.cauce.org/


Osmo

Dr John Stockton

unread,
Aug 10, 1997, 3:00:00 AM8/10/97
to

In article <19970806181...@ladder02.news.aol.com> of Wed, 6 Aug
1997 18:15:03 in comp.lang.pascal.borland, EMcCrosk <emcc...@aol.com>

wrote:
>
>I am wanting to find out how to reference a single bit in a byte.
>
>Say I have a variable B : byte. How would I find out what the value of
>the third bit (for example) of that variable would be? Also how would I
>set the value of a bit?

There is AFAIR no need to do any shifts at run-time.

See "Bit Testing" in http://www.merlyn.demon.co.uk/pas-time.htm#Opt
for some hints.


--
John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v1.12 MIME.
**** Merlyn was closed Jul 25 - Aug 9 ****
Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.

Claire Humphrey

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

if (b AND 4)
b := b OR 4

>In article <19970806181...@ladder02.news.aol.com> of Wed, 6 Aug
>1997 18:15:03 in comp.lang.pascal.borland, EMcCrosk <emcc...@aol.com>
>wrote:
>>
>>I am wanting to find out how to reference a single bit in a byte.
>>
>>Say I have a variable B : byte. How would I find out what the value of
>>the third bit (for example) of that variable would be? Also how would I
>>set the value of a bit?

--
Claire Humphrey

0 new messages