home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!sgiblab!munnari.oz.au!metro!grivel!metz.une.edu.au!ddavidso
- From: ddavidso@metz.une.edu.au (Dean Davidson)
- Newsgroups: comp.lang.pascal
- Subject: Re: need help with x to y power or 'setbit' function
- Message-ID: <2461@grivel.une.edu.au>
- Date: 12 Nov 92 01:24:09 GMT
- References: <BxBDzx.AD3@javelin.sim.es.com>
- Sender: usenet@grivel.une.edu.au
- Organization: University of New England, Armidale, Australia
- Lines: 81
-
- In article <BxBDzx.AD3@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
- >i'm using bits in an array of bytes as flags in the application i'm
- >developing. i bitest via bitest:=(byte and ($01 shl bit))<>0 but i'm having
- >trouble with my setbit and clearbit routines. my method of accomplishing
- >bitsetting was going to be done by testing the desired bit of x, and if the
- >result is false to add 2 to the bit power to x. clearbit would obviously be
- >just the converse.
- >
- Here is the unit I use:
-
- Thanks to Trevor Carlsen for some of these
-
-
- unit bituni;
-
- interface
-
- {32 bit (longint) routines}
- function b_test( numb : longint ; posn : byte) : boolean;
- procedure b_set( var numb : longint ; posn : byte);
- procedure b_toggle(var numb : longint ; posn : byte);
- procedure b_reset( var numb : longint ; posn : byte);
-
- {8 bit (byte) routines}
- function BitIsSet(numb,bitnumb : byte): boolean;
- {To test a bit in a byte - Tests bit bitnumb in numb}
- procedure SetBit(var numb : byte; bitnumb : byte);
- {To set a bit in a byte - Sets bit bitnumb in numb}
- procedure ResetBit(var numb : byte; bitnumb : byte);
- {To reset a bit in a byte - Resets bit bitnumb in numb}
-
- implementation
-
- function b_test(numb : longint ; posn : byte) : boolean;
- begin
- b_test := odd(numb shr posn);
- end;
-
- procedure b_set(var numb : longint ; posn : byte);
- begin
- numb := numb or (1 shl posn);
- end;
-
- procedure b_toggle(var numb : longint ; posn : byte);
- begin
- numb := numb xor (1 shl posn);
- end;
-
- procedure b_reset(var numb : longint ; posn : byte);
- begin
- numb := (numb or (1 shl posn)) xor (1 shl posn);
- end;
-
- {-----BYTE OPS-----}
-
- function BitIsSet(numb,bitnumb : byte): boolean;
- {To test a bit in a byte - Tests bit bitnumb in numb}
- begin
- BitIsSet := odd(numb shr bitnumb);
- end;
-
- procedure SetBit(var numb : byte; bitnumb : byte);
- {To set a bit in a byte - Sets bit bitnumb in numb}
- begin
- numb := numb or (1 shl bitnumb);
- end;
-
- procedure ResetBit(var numb : byte; bitnumb : byte);
- {To reset a bit in a byte - Resets bit bitnumb in numb}
- begin
- numb := numb and not (1 shl bitnumb);
- end;
-
- begin
- end.
-
- --
- Dean Davidson ddavidso@metz.une.oz.au
- Dept Psychology Phone 61 67 73 2585
- University of New England Fax 61 67 72 9816
- Armidale NSW 2351 Australia VK2 ZID
-