![]() |
Forum Index : Microcontroller and PC projects : Need some "Bit Manipulation" Help
Author | Message | ||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 327 |
I need some help, my mind can not seem to come up with a easy solution to this. ![]() For a given byte of data, I need to “randomly” create two bit masks (may not be the correct term for them) based on the following rules: 1) If a bit is off in the original byte it must be off in both mask bytes 2) Random bits need to be turned off in both masks, however the same bit must not be turn off in both masks. 3) It is okay if a bit is unchanged (remains on) in both masks. You should be able to take the two bit mask bytes and OR them together to reproduce the original byte. Any ideas? Thanks, --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6266 |
A start: DO INPUT "Number (0 to 255)"; x y = 0:z = 0 FOR n = 0 TO 7 IF (x AND 2^(n)) > 0 THEN ' bit is set IF RND() > 0.5 THEN 'choose which one to set y =y+ 2^(n) ELSE z = z + 2^(n) ENDIF ENDIF NEXT n PRINT x,y,z, y OR z LOOP You could change the RND() statement to have 3 choices, the third one setting both bits. Jim VK7JH MMedit |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 327 |
Thanks Jim! Ran a quick test and it looks like it will give me the results I'm looking for. ![]() Thanks again, --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 327 |
Oops! It's not working. A closer examination of the test data reveals the program is not allowing for the same bit to be high in both mask bytes. The only condition that is not allowed is the same bit can not be zero in both mask bytes. --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6266 |
DO INPUT "Number (0 to 255)"; x y = 0:z = 0 FOR n = 0 TO 7 IF (x AND 2^(n)) > 0 THEN ' bit is set IF RND() > 0.667 THEN 'third of the time the bit in 'y' is set y =y + 2^(n) ELSEIF RND() > 0.5 THEN 'third of the time the bit in 'z' is set z = z + 2^(n) ELSE 'third of the time the bits in 'y' and 'z' are set y =y + 2^(n) z = z + 2^(n) ENDIF ENDIF NEXT n PRINT x,y,z, y OR z PRINT "x = "+RIGHT$("00000000"+BIN$(x),8) ' V5.1 can use BIN$(x,8) PRINT "y = "+RIGHT$("00000000"+BIN$(y),8) PRINT "z = "+RIGHT$("00000000"+BIN$(z),8) PRINT "OR = "+RIGHT$("00000000"+BIN$(y OR z),8) LOOP You can vary the percent for each condition to suit. Jim Edited to display output in BIN format. I am using V4.5 for DOS so had to stick with the old method of padding the BIN output to 8 bits. VK7JH MMedit |
||||
Justplayin![]() Guru ![]() Joined: 31/01/2014 Location: United StatesPosts: 327 |
This version looks very good and all the bits appear to fits criteria. I think I can easily adapt this into my program. Thanks! --Curtis I am not a Mad Scientist... It makes me happy inventing new ways to take over the world!! |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |