Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Bit masking?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Bit masking?

by rizla420 on Wed Aug 25, 2010 10:19 am

Whats the best way to go about inverting all the bits in a byte of a variable and copying the result to another.

something like:

byte original_var=10101010;
byte bit_mask=00000000;

output_var=original_var & bit_mask;

//im assuming the output_var should have this value. Do i have to loop through the mask, or just by having it expressed the way i have it should have the expected result?

output_var=01010101;

I cant check my code now , but just wanted to see if thats the proper way to do it.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Bit masking?

by adafruit_support_bill on Wed Aug 25, 2010 10:32 am

'~' is the bitwise NOT operator in C/C++.

as in:

output = ~input;

adafruit_support_bill
 
Posts: 77911
Joined: Sat Feb 07, 2009 10:11 am

Re: Bit masking?

by rizla420 on Wed Aug 25, 2010 10:55 am

arduwino wrote:'~' is the bitwise NOT operator in C/C++.

as in:

output = ~input;


I just read that on the arduino page.. that is such a helpful operator. Thanks!

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Bit masking?

by pstemari on Wed Aug 25, 2010 11:59 am

Alternatively, you can use ^ (XOR) to flip individual bits, e.g.

byte x = 0x5A;
x ^= 0x0F;
x = 0x55;
--Paul

A wholly owned subsidiary of:
Persephone: DL R+W+B C 7 X L W C++ I++ T+ A E H++ S+ V-- F+ Q P B PA+ PL
Aldebaran: DM Rt H 5 Y L- W+ C+ I++ T++ A+++ E H++ S+ V+ F++ Q+ P B++ PA- PL--
User avatar
pstemari
 
Posts: 310
Joined: Sun Mar 21, 2010 6:10 pm
Location: Seattle, WA

Re: Bit masking?

by rizla420 on Wed Aug 25, 2010 12:29 pm

pstemari wrote:Alternatively, you can use ^ (XOR) to flip individual bits, e.g.

byte x = 0x5A;
x ^= 0x0F;
x = 0x55;


I thought that was what the ~ did.. hm i'll have to compare the output of both. I just need to flip the bits in a byte value, one of one. Perferably without have to loop through each bit in the byte.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Bit masking?

by westfw on Wed Aug 25, 2010 12:47 pm

"~" ("not") inverts all the bits in the argument. "^" ("exclusive or") will only invert the bits that are the same in both arguments:
Code: Select all | TOGGLE FULL SIZE
~1 == 0
~0 == 1
1 ^ 0 == 1
1 ^ 1 == 0
0 ^ 0 == 0
0 ^ 1 == 1

This allows you to select only some bits in a variable to be inverted:
result = var ^ bitmaskOfBitsToBeInverted;
(leaving the bits that weren't 1s in the bitmask the same as they were.)

westfw
 
Posts: 1652
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Bit masking?

by rizla420 on Wed Aug 25, 2010 12:56 pm

westfw wrote:"~" ("not") inverts all the bits in the argument. "^" ("exclusive or") will only invert the bits that are the same in both arguments:
Code: Select all | TOGGLE FULL SIZE
~1 == 0
~0 == 1
1 ^ 0 == 1
1 ^ 1 == 0
0 ^ 0 == 0
0 ^ 1 == 1

This allows you to select only some bits in a variable to be inverted:
result = var ^ bitmaskOfBitsToBeInverted;
(leaving the bits that weren't 1s in the bitmask the same as they were.)


Thanks westfw for the clarification. So the NOT operator does an exact inversion of all the bits. The XOR relies upon a second argument as a comparator to decide which bits to flip. Sound about right?

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Bit masking?

by zener on Wed Aug 25, 2010 1:25 pm

Yes. Wherever there is a "1" in the mask, it flips that bit. I think this is what you are wanting. Yes?

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Bit masking?

by rizla420 on Wed Aug 25, 2010 1:45 pm

Zener wrote:Yes. Wherever there is a "1" in the mask, it flips that bit. I think this is what you are wanting. Yes?


Yea, but i really wasn't going to be using a mask. I just thought thats how you had to do it, similar to network subnetting. I just needed a way to invert the bits of a byte. If the value is 1 change to 0, vice versa. So i think the NOT operator is more so what i'm looking for rather than the XOR.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Please be positive and constructive with your questions and comments.