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

evaluate bits in a byte

5 views
Skip to first unread message

Si Ballenger

unread,
May 14, 2005, 12:37:29 AM5/14/05
to
I've done some tinkering with a micro controller that will send
back a byte when requested indicating weather the eight input
pins on the chip are 0v or +5v. I save the returned byte (in
decimal form) in a file called byte.txt and use the below batch
file to tell me what the pin status is. Thought this might be of
interest to others that are tinkering with micro controllers and
such. I've partly got it working sending the status back to a
brouser as a web page.

@echo off & setlocal enableextensions
set /p a=<byte.txt
echo the byte is %a%
echo current pin status:
SET /A b= (%a% "&" 128)
if %b% EQU 0 (echo pin 8 is off) else echo pin 8 is on
SET /A b= (%a% "&" 64)
if %b% EQU 0 (echo pin 7 is off) else echo pin 7 is on
SET /A b= (%a% "&" 32)
if %b% EQU 0 (echo pin 6 is off) else echo pin 6 is on
SET /A b= (%a% "&" 16)
if %b% EQU 0 (echo pin 5 is off) else echo pin 5 is on
SET /A b= (%a% "&" 8)
if %b% EQU 0 (echo pin 4 is off) else echo pin 4 is on
SET /A b= (%a% "&" 4)
if %b% EQU 0 (echo pin 3 is off) else echo pin 3 is on
SET /A b= (%a% "&" 2)
if %b% EQU 0 (echo pin 2 is off) else echo pin 2 is on
SET /A b= (%a% "&" 1)
if %b% EQU 0 (echo pin 1 is off) else echo pin 1 is on
endlocal
pause


William Allen

unread,
May 14, 2005, 3:13:07 AM5/14/05
to
"Si Ballenger" wrote in message

> I've done some tinkering with a micro controller that will send
> back a byte when requested indicating weather the eight input
> pins on the chip are 0v or +5v. I save the returned byte (in
> decimal form) in a file called byte.txt and use the below batch
> file to tell me what the pin status is.
...snip

This variant avoids repeated code lines, and displays the status
as a two-line table that may be easier to read. Reads byte value
from command-line parameter (for testing), or from BYTE.TXT
if no parameter supplied.

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
IF "%1"=="" (SET /p Byte=<BYTE.TXT) ELSE (SET Byte=%1)
(SET Test=128) & (SET Stat= )
:LOOP
SET /a Bstat=%Byte% "&" %Test%
IF %Bstat% EQU 0 (SET Stat=%Stat% OFF) ELSE (SET Stat=%Stat% ON)
SET /a Test=%Test% / 2
IF NOT %Test% EQU 0 GOTO LOOP
ECHO. Pin: 8 7 6 5 4 3 2 1
ECHO. %Stat%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Read the following screen capture in fixed-width font:

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo 0
Pin: 8 7 6 5 4 3 2 1
OFF OFF OFF OFF OFF OFF OFF OFF

C:\WORK>demo 255
Pin: 8 7 6 5 4 3 2 1
ON ON ON ON ON ON ON ON

C:\WORK>demo 78
Pin: 8 7 6 5 4 3 2 1
OFF ON OFF OFF ON ON ON OFF

C:\WORK>type byte.txt
255

C:\WORK>demo
Pin: 8 7 6 5 4 3 2 1
ON ON ON ON ON ON ON ON

C:\WORK>
============End screen capture

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/


Si Ballenger

unread,
May 14, 2005, 2:00:00 PM5/14/05
to

Your works well and I can echo it to a text file. Using a text
file in my cgi setup apparently has some issues right now with
IE, so I'm dropping back to a html setup. You should be able to
put the below link in your brouser and get the 170 byte on the
link query_stirng evaluated. You can change the 170 to other byte
values and they should evaluate also. Bottom is the current
bytetest batch file. In the end I'll add an application to the
bach file that first sends a byte to a micro controller, then
receives a status byte back and it gets saved in the byte.txt
file for evaluation.

http://zoomkat.d2g.com:88/cgi-bin/bytetest.bat?170

========bytetest.bat==================

@Echo OFF
Echo Content-type: text/html
Echo.
echo %query_string% >byte.txt


@echo off & setlocal enableextensions
set /p a=<byte.txt

SET /A b= (%a% "&" 128)

if %b% EQU 0 (set x1=off) else set x1=on

SET /A b= (%a% "&" 64)

if %b% EQU 0 (set x2=off) else set x2=on


SET /A b= (%a% "&" 32)

if %b% EQU 0 (set x3=off) else set x3=on

SET /A b= (%a% "&" 16)

if %b% EQU 0 (set x4=off) else set x4=on

SET /A b= (%a% "&" 8)

if %b% EQU 0 (set x5=off) else set x5=on

SET /A b= (%a% "&" 4)

if %b% EQU 0 (set x6=off) else set x6=on

SET /A b= (%a% "&" 2)

if %b% EQU 0 (set x7=off) else set x7=on

SET /A b= (%a% "&" 1)

if %b% EQU 0 (set x8=off) else set x8=on

Echo ^<HTML^>
Echo ^<HEAD^>
Echo ^<TITLE^>Bit Status^</title^>
Echo ^</head^>
Echo ^<BODY^>
Echo ^<H1^>Bit Status^</h1^>
echo Today's date is ^%date%^<BR^>
echo Current time is %time%^<BR^>
echo Your IP address is %REMOTE_ADDR%^<BR^>
Echo Initiating command now:^<BR^>
echo the byte is %a% ^<BR^>
echo ^<B^>current pin status:^</B^> ^<BR^>
echo 1 is %x1% ^<BR^>
echo 2 is %x2% ^<BR^>
echo 3 is %x3% ^<BR^>
echo 4 is %x4% ^<BR^>
echo 5 is %x5% ^<BR^>
echo 6 is %x6% ^<BR^>
echo 7 is %x7% ^<BR^>
echo 8 is %x8% ^<BR^>

Echo Did it work?^<BR^>
Echo ^</body^>
Echo ^</html^>
endlocal
::del byte.txt

Clay Calvert

unread,
May 14, 2005, 10:14:56 PM5/14/05
to
On Sat, 14 May 2005 18:00:00 GMT, shb*NO*SPAM*@comporium.net (Si
Ballenger) wrote:

>Your works well and I can echo it to a text file. Using a text
>file in my cgi setup apparently has some issues right now with
>IE, so I'm dropping back to a html setup. You should be able to
>put the below link in your brouser and get the 170 byte on the
>link query_stirng evaluated. You can change the 170 to other byte
>values and they should evaluate also. Bottom is the current
>bytetest batch file. In the end I'll add an application to the
>bach file that first sends a byte to a micro controller, then
>receives a status byte back and it gets saved in the byte.txt
>file for evaluation.
>
>http://zoomkat.d2g.com:88/cgi-bin/bytetest.bat?170
>
>========bytetest.bat==================

Si, you're webpage worked for me. Thank you also for showing a
working "bitwise" comparison batch. I never had any luck with that
type of operation. You've inspired me to revist other Set /A
features. Since you are using at least 2000, here is another variant
that uses Logical Shift functionality.

@echo off & setlocal enabledelayedexpansion
SET /p Byte=<BYTE.TXT
Set Lshift=128
for /l %%a in (8,-1,1) do (
SET /A bit=%Byte% "&" !Lshift!
if !bit! EQU 0 (echo pin %%a is off) else echo pin %%a is on
set /a Lshift ">>=" 1)


Since your web page displays the bit order from 1 - 8 here is another
version that reverses the order of bit testing.


@echo off & setlocal enabledelayedexpansion
SET /p Byte=<BYTE.TXT
Set Lshift=1
for /l %%a in (1,1,8) do (
SET /A bit=%Byte% "&" !Lshift!
if !bit! EQU 0 (echo pin %%a is off) else echo pin %%a is on
set /a Lshift "<<=" 1)

Thanks

Clay Calvert
CCal...@Wanguru.com
Replace "W" with "L"

Si Ballenger

unread,
May 14, 2005, 11:57:39 PM5/14/05
to

I noticed that so I just revrsed the order of the "x" variables
in the evaluation sequence so now a 128 byte would say 8 is on
and 1-7 are off. In the real world it will report something like
below.

window #1 is open
window #2 is open
window #3 is closed
window #4 is closed
garage door is closed
TV is on
Radio is off

>@echo off & setlocal enabledelayedexpansion
>SET /p Byte=<BYTE.TXT
>Set Lshift=1
>for /l %%a in (1,1,8) do (
> SET /A bit=%Byte% "&" !Lshift!
> if !bit! EQU 0 (echo pin %%a is off) else echo pin %%a is on
> set /a Lshift "<<=" 1)

I'm saving all the various followup post for possible future
useage. I'm use to seeing a 0 or a 1 in a bit comparison, so it
caught me by supprise when either a 0 or the value of the
comparison bit was returned. I'm interested in the math
operations available for positioning servos and such by saving a
position in a text file, then reclaiming it and adding to or
subtracting from the saved value and using that for the next
position value. I noticed when I use 255 in place of the 170 in
the url, all are shown as on as expected. If I use 256, all are
shown off, and with 257 the first is shown as on and the rest are
off. Looks like any value beyone 255 starts evaluating the
supplied byte minus 256 and such.

Timo Salmi

unread,
May 15, 2005, 2:31:36 AM5/15/05
to
Clay Calvert wrote:
> shb*NO*SPAM*@comporium.net (Si Ballenger) wrote:
(snip)

> Si, you're webpage worked for me. Thank you also for showing a
> working "bitwise" comparison batch. I never had any luck with
> that type of operation. You've inspired me to revist other Set /A
> features. Since you are using at least 2000, here is another
> variant that uses Logical Shift functionality.

Nice. Anyway, this is and has been an exercise which brings to mind
the benefits off cross-fertilization over very different systems. The
bitwise comparison was one of the main tricks in the Vic-20 /
Commodore 64 era. Done so many times. The same tasks sometimes
surface and we have to reinvent the wheel with different tools. There
is a certain nostalgia in seeing the similarity.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Timo Salmi

unread,
May 20, 2005, 9:47:20 PM5/20/05
to
Si Ballenger wrote:
> I've done some tinkering with a micro controller that will send
> back a byte when requested indicating weather the eight input
> pins on the chip are 0v or +5v. I save the returned byte (in

One will realize in retrospect that this is the same task (with the
byte order reversed) as converting a number into a binary. That and
related we have tackled previously e.g. in

32} How can I convert a decimal number to binary, octal, hexadecimal?
73} How can I convert a hexadecimal string into an ASCII string?
88} How to convert DEC to HEX with a script file, and vice versa?

But there is nothing dramatic or unexpected in the same tasks
surfacing in slightly different clothes.

Si Ballenger

unread,
May 20, 2005, 11:30:50 PM5/20/05
to
On Sat, 21 May 2005 04:47:20 +0300, Timo Salmi <t...@uwasa.fi>
wrote:

>Si Ballenger wrote:
>> I've done some tinkering with a micro controller that will send
>> back a byte when requested indicating weather the eight input
>> pins on the chip are 0v or +5v. I save the returned byte (in
>
>One will realize in retrospect that this is the same task (with the
>byte order reversed) as converting a number into a binary. That and
>related we have tackled previously e.g. in
>
>32} How can I convert a decimal number to binary, octal, hexadecimal?
>73} How can I convert a hexadecimal string into an ASCII string?
>88} How to convert DEC to HEX with a script file, and vice versa?
>
>But there is nothing dramatic or unexpected in the same tasks
>surfacing in slightly different clothes.

I'd venture to say that FAQ 1 thru 98 all based on bitwise
evaluations of bytes (as does pretty much everything that goes on
in your computer). The trick is to get the simplest and most
straight foward solution to the problem presented. That is
probably why 32, 73, and 88 exist instead of just one of them.

Timo Salmi

unread,
May 21, 2005, 1:23:12 AM5/21/05
to
William Allen wrote:
> "Si Ballenger" wrote in message
>>I've done some tinkering with a micro controller that will send
>>back a byte when requested indicating weather the eight input
>>pins on the chip are 0v or +5v. I save the returned byte (in

> ECHO. Pin: 8 7 6 5 4 3 2 1
> ECHO. %Stat%

Not that it matters, but the range convention often goes from
0 to 7 rather than 1 to 8. Anyway, this is what I added to the
FAQ item


32} How can I convert a decimal number to binary, octal, hexadecimal?

There are so many different options to perform a task. Here is
another way to convert a 8-bit decimal to its binary equivalent.
The formulation below emphasizes extracting each bit individually.
@echo off & setlocal enableextensions enabledelayedexpansion
set decimal_=%1
set /a b0=%decimal_% ^& 1
set /a b1=%decimal_% ^& 2
set /a b2=%decimal_% ^& 4
set /a b3=%decimal_% ^& 8
set /a b4=%decimal_% ^& 16
set /a b5=%decimal_% ^& 32
set /a b6=%decimal_% ^& 64
set /a b7=%decimal_% ^& 128
set i=-1
for %%b in (!b0! !b1! !b2! !b3! !b4! !b5! !b6! !b7!) do (
set bit=%%b
set /a i+=1
if !bit! EQU 0 (echo bit !i! = 0) else (echo bit !i! = 1)
)
endlocal & goto :EOF
An example of the output
C:\_D\TEST>cmdfaq 12
bit 0 = 0
bit 1 = 0
bit 2 = 1
bit 3 = 1
bit 4 = 0
bit 5 = 0
bit 6 = 0
bit 7 = 0

References/Comments:
http://www.google.com/groups?selm=4285a523$0$93767$ed26...@ptn-nntp-reader01.plus.net
http://www.google.com/groups?selm=nfbd81pp3ebfjjdoq...@4ax.com

Si Ballenger

unread,
May 21, 2005, 9:13:15 AM5/21/05
to
On Sat, 21 May 2005 08:23:12 +0300, Timo Salmi <t...@uwasa.fi>
wrote:

>32} How can I convert a decimal number to binary, octal, hexadecimal?


>
>There are so many different options to perform a task. Here is
>another way to convert a 8-bit decimal to its binary equivalent.
>The formulation below emphasizes extracting each bit individually.
> @echo off & setlocal enableextensions enabledelayedexpansion
> set decimal_=%1
> set /a b0=%decimal_% ^& 1
> set /a b1=%decimal_% ^& 2
> set /a b2=%decimal_% ^& 4
> set /a b3=%decimal_% ^& 8
> set /a b4=%decimal_% ^& 16
> set /a b5=%decimal_% ^& 32
> set /a b6=%decimal_% ^& 64
> set /a b7=%decimal_% ^& 128
> set i=-1
> for %%b in (!b0! !b1! !b2! !b3! !b4! !b5! !b6! !b7!) do (
> set bit=%%b
> set /a i+=1
> if !bit! EQU 0 (echo bit !i! = 0) else (echo bit !i! = 1)
> )
> endlocal & goto :EOF

I think the title of faq 32 is somewhat misleading for describing
the problem that is being addressed. I think the FAQ could
support a stand alone bit evaluation title seeing as it already
supports three seperate HEX/DEC/OCT/BIN conversion titles for
basically doing the same thing. I would also suggest you include
some of the below evaluation format, as it actually the format
that is described in cmd help for SET and IF, and it shows how to
a simple single bit evaluation if desired without having to
involve a "do loop" to obtain an output (the "kiss" approach).

@echo off & setlocal enableextensions

set /p a=12


SET /A b= (%a% "&" 128)
if %b% EQU 0 (echo pin 8 is off) else echo pin 8 is on
SET /A b= (%a% "&" 64)
if %b% EQU 0 (echo pin 7 is off) else echo pin 7 is on
SET /A b= (%a% "&" 32)
if %b% EQU 0 (echo pin 6 is off) else echo pin 6 is on
SET /A b= (%a% "&" 16)
if %b% EQU 0 (echo pin 5 is off) else echo pin 5 is on
SET /A b= (%a% "&" 8)
if %b% EQU 0 (echo pin 4 is off) else echo pin 4 is on
SET /A b= (%a% "&" 4)
if %b% EQU 0 (echo pin 3 is off) else echo pin 3 is on
SET /A b= (%a% "&" 2)
if %b% EQU 0 (echo pin 2 is off) else echo pin 2 is on
SET /A b= (%a% "&" 1)
if %b% EQU 0 (echo pin 1 is off) else echo pin 1 is on
endlocal

>References/Comments:
>http://www.google.com/groups?selm=4285a523$0$93767$ed26...@ptn-nntp-reader01.plus.net
>http://www.google.com/groups?selm=nfbd81pp3ebfjjdoq...@4ax.com

Why not add the whole thread as a reference? All variations would
shown, not just "selected" ones which might be a little more
complex than needed by the end user.

http://groups-beta.google.com/group/alt.msdos.batch.nt/browse_thread/thread/459f7ffcc29be5b2/da6ac32a0cc66e21

Just another outside view from a non batch expert..

Timo Salmi

unread,
May 21, 2005, 11:38:39 AM5/21/05
to
Si Ballenger wrote:
> Timo Salmi <t...@uwasa.fi> wrote:
>> 32} How can I convert a decimal number to binary, octal,
>> hexadecimal? There are so many different options to perform a
>> task. Here is another way to convert a 8-bit decimal to its
>> binary equivalent. The formulation below emphasizes extracting
>> each bit individually.

> I think the title of faq 32 is somewhat misleading for describing


> the problem that is being addressed. I think the FAQ could

Thanks for the useful feedback, Si. At the very least I'll think
about your welcome suggestion.

> Why not add the whole thread as a reference? All variations would

This I have though about several times with the other items and
decided against referring to entire threads in the FAQ. Why? First,
one can get to the treads from the individual postings. Second,
threads usually are full of development discussions and often the
early drafts. They can even contain non-working or outright wrong
solutions along the way. Third, threads can be very long and the
newcomer reader might get quite lost where the essence lies. All in
all, my choice is hence to point to some essential picks.

Timo Salmi

unread,
May 21, 2005, 4:32:03 PM5/21/05
to
Si Ballenger wrote:
> Timo Salmi <t...@uwasa.fi> wrote:
>>32} How can I convert a decimal number to binary, octal, hexadecimal?

> I think the title of faq 32 is somewhat misleading for describing


> the problem that is being addressed. I think the FAQ could
> support a stand alone bit evaluation title seeing as it already

DRAFT

102) How can I extract the individual bits from a decimal byte?

Basically this is the same task as the decimal to binary
conversion in item #32. The formulation below emphasizes extracting
each bit of a decimal byte individually. A byte contains 8 bits,
usually labeled from 0 to 7. It also shows that are many different
options to perform a task since the solution here differs
considerably from the one used in item #32.
These bitwise operations were the key elements of programming
back in the days of Vic 20 and Commodore 64, but can still arise in
the present day as seen in the references.

@echo off & setlocal enableextensions enabledelayedexpansion
set decimal_=%1
set /a b0=%decimal_% ^& 1
set /a b1=%decimal_% ^& 2
set /a b2=%decimal_% ^& 4
set /a b3=%decimal_% ^& 8
set /a b4=%decimal_% ^& 16
set /a b5=%decimal_% ^& 32
set /a b6=%decimal_% ^& 64
set /a b7=%decimal_% ^& 128
set i=-1
for %%b in (!b0! !b1! !b2! !b3! !b4! !b5! !b6! !b7!) do (
set bit=%%b
set /a i+=1
if !bit! EQU 0 (echo bit !i! = 0) else (echo bit !i! = 1)
)
endlocal & goto :EOF

An example of the output
C:\_D\TEST>cmdfaq 12
bit 0 = 0
bit 1 = 0
bit 2 = 1
bit 3 = 1
bit 4 = 0
bit 5 = 0
bit 6 = 0
bit 7 = 0

Examples of bitwise operations


@echo off & setlocal enableextensions

set dec1_=121
set dec2_=145
::
set /a decand_=%dec1_% ^& %dec2_%
echo %dec1_% AND %dec2_% = %decand_%
::
set /a decor_=%dec1_% ^| %dec2_%
echo %dec1_% OR %dec2_% = %decor_%
::
set /a decxor_=%dec1_% ^^ %dec2_%
echo %dec1_% XOR %dec2_% = %decxor_%
::
set /a decnot_=%dec1_% ^^ 255
echo NOT %dec1_% = %decnot_%
::
set dec3_=1
set shft_=3
set decshl_=%dec3_%
set /a decshl_ ^<^<= %shft_%
echo %dec3_% SHL %shft_% = %decshl_%
::
set dec4_=128
set shft_=1
set decshr_=%dec4_%
set /a decshr_ ^>^>= %shft_%
echo %dec4_% SHR %shft_% = %decshr_%
::
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
121 AND 145 = 17
121 OR 145 = 249
121 XOR 145 = 232
NOT 121 = 134
1 SHL 3 = 8
128 SHR 1 = 64
The SHL (shift left) can be used to get the powers of 2.

All the best, Timo

Si Ballenger

unread,
Jun 1, 2005, 11:52:19 PM6/1/05
to
On Sat, 21 May 2005 23:32:03 +0300, Timo Salmi <t...@uwasa.fi>
wrote:

>Si Ballenger wrote:


>> Timo Salmi <t...@uwasa.fi> wrote:
>>>32} How can I convert a decimal number to binary, octal, hexadecimal?
>
>> I think the title of faq 32 is somewhat misleading for describing
>> the problem that is being addressed. I think the FAQ could
>> support a stand alone bit evaluation title seeing as it already
>
>DRAFT
>
>102) How can I extract the individual bits from a decimal byte?

You might be able to expand that to decimal or hex byte. Hex
values also work in my setup when the byte is expressed in the
form of 0xff. The example links below return the same result.

http://zoomkat.d2g.com:88/cgi-bin/bytetest.bat?170
http://zoomkat.d2g.com:88/cgi-bin/bytetest.bat?0xaa

Timo Salmi

unread,
Jun 3, 2005, 2:04:33 PM6/3/05
to
Si Ballenger <shb*NO*SPAM*@comporium.net> wrote:
> Timo Salmi <t...@uwasa.fi> wrote:
> >> Timo Salmi <t...@uwasa.fi> wrote:
> >>>32} How can I convert a decimal number to binary, octal, hexadecimal?

> >DRAFT


> >102) How can I extract the individual bits from a decimal byte?

> You might be able to expand that to decimal or hex byte. Hex

Exactltly so. But in a FA>Q it sometimes is enough just to get the
user started, not necessarily cover all the eventualities. A good
tip though, Si.

0 new messages