Bit Manipulation

November 10, 2009 at 06:40:58
Specs: Windows Vista
Hi, can anyone tell me how to insert a parity bit in a 7-bit value (in 4th position)?

See More: Bit Manipulation

Report •

November 10, 2009 at 13:21:27
using A for the var. you want to "insert" at the 4th bit (i will risk the assumption that you mean switch and not insert) and B as the var. that is holding the parity bit:

:assign "whatever" value to A as hex, max. 7F=127
set /a A=0x77
:set B to hex 8 will switch bit 3 (4th bit from right) ON
set /a B=0x8
set /a "A=%A%|%B%"
echo %A%
:now set B to switch the bit OFF
set /a B=0x77
set /a "A=%A%&%B%"
:now it's switched off
echo %A%

where B is 8 to switch bit 3 ON using OR, or B is 77 to switch bit 3 OFF using AND.
i was thinking a single operation could handle both ON and OFF, but i haven't figured it out

Report •

November 10, 2009 at 14:15:03
Thank you very much. I'll try this

Report •

November 10, 2009 at 17:37:56
I got to thinking:
a slightly more simple method might be to switch the bit OFF using a constant, then use B to switch it on/off by adding B:
set /a A=0x77
:foll switches the bit OFF
set /a A="%A%&0x77"
:then just add B where B is 0 for parity even or 8 for parity odd
set /a A=%A%+%B%

that eliminates B having to have any other value than 0 or 8
and having to do the OR operation. at least, i think this will work.

Report •

Related Solutions

November 11, 2009 at 14:47:05
Are you using any particular language?

From your description, I guess you have a 7-bit value stored
inside an 8-bit byte, with only the lowest 7 bits holding the
value and the 8th bit is ignored.

You then want to calculate the parity bit and insert it in bit 4.
So you need to leave the lowest 3 bits as they are, then the
next four bits need to be shifted left 1 bit so that you can set
the 4th bit as the parity bit.

If you are using C, you should use an unsigned char. You can
either do it using the operators <<, & and |, or you can cheat
by putting the unsigned char inside a union with a bitfield.

Report •

Ask Question