Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:56 01 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Need some "Bit Manipulation" Help

Author Message
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 327
Posted: 10:08am 07 Apr 2016
Copy link to clipboard 
Print this post

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: Australia
Posts: 6266
Posted: 12:49pm 07 Apr 2016
Copy link to clipboard 
Print this post

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 States
Posts: 327
Posted: 01:07pm 07 Apr 2016
Copy link to clipboard 
Print this post

Thanks Jim!

Ran a quick test and it looks like it will give me the results I'm looking for. Don't know why, but my mind went blank on this one when I tried to work it out for myself.

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 States
Posts: 327
Posted: 02:09pm 07 Apr 2016
Copy link to clipboard 
Print this post

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: Australia
Posts: 6266
Posted: 04:08pm 07 Apr 2016
Copy link to clipboard 
Print this post


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
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.
Edited by TassyJim 2016-04-09
VK7JH
MMedit
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 327
Posted: 09:52am 08 Apr 2016
Copy link to clipboard 
Print this post

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!!
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025