Programming: Storing bitmask

Wish there was a google for simple concepts, but think this is the type of thing that is easier to ask.

Trying to return a byte with the highest bits masked out. So 0b1000-1010 & 0b1100-0000 would become 0b0000-1010.

So far I have just done comparisons with mask using (&) I noticed if I try to store the masked value to a variable it just evaluates as true. ( myVar = input & mask; myVar == 1 ) Makes sense to me that this is the wrong approach, its just a comparison. The question is, what is the syntax for creating a new value with desired bits masked out?

Storing bitmasks made easy: :smile:

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
3 Likes

@developstuff 's macros are a good and well known way to manipulate and test bit values :+1:

On the other hand I think there is something sort of obscure here (or I don’t understand the question :blush: ):

@inof8or, I can’t quite follow this example.

  0b10001010
& 0b11000000

would actually work out as 0b10000000
To get 0b00001010 by masking out the two MSBs, you’d need to & with 0b00111111 (= ~0b11000000).

And as for the storing the masked value myVar = (input & mask); will actually give you 0b00001010 for the above example. If you test if (myVar) you will get a true for any value not zero. But if you explicitly test if (myVar == 0b1010) you will only get true for this one value.

Curious how this would work in code

@developstuff Thanks! Already had this set up for my button state detection. Should have been included like it is with Arduino

As it turns out was approaching my problem the wrong way, so “storing” the mask is unnecessary. Ended up just needing a couple of bitWrite()

Because of this

Was a bit confused, it was also reading as 1 in serial.print(); statements when “myVar” was returned. Still curious how to store the result of the mask. Though there may be no application for it.

@ScruffR Nevermind, my flow control must have been messed up

If one does the following,

byte myVar = 0b10001001;
  myVar = myVar & 0b00111111;
  Serial.println(myVar, BIN);

The result is,

1001

Sorry, had a different result with the function I was testing.

When you say highest bits, do you mean the upper 4 bits, aka upper nibble?

If so, ANDing with 0b00001111 will do the job for 8-bit numbers.

uint8_t myVal = 0b11111111;
mvVal &= 0b00001111; // mvVal now contains 0b00001111

1 ANDed with any bit equals whatever that bit is, 0 ANDed with any bit always equals 0.

This was one of those things where everything cleared up once the right questions were asked as opposed to throwing around hypothesis. The function that was created ended up looking like this-

byte charToPattern(byte letter)
{
  // Space also doubles as the first shift in a chord          
  if(letter == 32) { return 64; } //Express convertion: Space 
  
  // for all of the key mapping
  for (byte i=0; i<PATTERNSIZE; i++)   
  {
    // return typicall letter patterns
    if ( letter == ('a'+ i) ) {
      return chordPatterns[i];
    }
    
    // in numbers shift case return pattern with 6th bit shift
    if ( letter < 58 && letter == ('0' + i) ) {
      return chordPatterns[i] | 64;
    } 
    
    // k-y cases ( !"#$%&'()*+'-./ )return 6th bit shift
    if ( letter > 32 && letter < 48 && letter == (23 + i) ) {
      return chordPatterns[i] | 64;
    }
    
    // a-g cases  (:;<=>?@ ), return 7th bit shift
    if ( letter < 65 && letter == (':' + i) ) {
      return chordPatterns[i] | 128;
    }
    
    // h-m cases  ([\]^_`  ), return 7th bit shift
    if ( letter > 90 && letter < 97 && letter == (84 + i) ) {
      return chordPatterns[i] | 128;
    }
    
    // n-q cases( {|}~   ), return 7th bit shift
    if ( letter > 122 && letter < 127 && letter == (110 + i) ) {
      return chordPatterns[i] | 128;
    }
  }
  return 0;
}

EDIT: I updated your code formatting, edit your post to see how to do it - BDub

Sorry md kinda sucks for keeping code formatting, that looks awful. Anyhow end up using OR instead of bitWrite(). Works in this situation because nether of the bits I want to mess with are ever high to begin with.

Also I want to reiterate the premise to the initial question is completely invalid because of some fluke output. I figured it out! Thanks for your time

1 Like

Still not sure what you’re up to but I guess you figured it out… have fun!

2 Likes

@BDub Thanks for fixing my post, looks a ton better. I’ll take more time to make sure code shows up like that in the future. Have been one lining single instruction flow to compress things because of the friction in spreading out across multiple pages. A lot of the functions inter-depend on one another which is no biggy across multiple pages in the arduino ide, but for spark its the prim and proper way so I have to be careful making other files to organize functions.

Did get everything resolved! Try to break things down when in a rut but maybe should just git branch the next time breaking down new concepts to keep things in context. It’s the type of project will get a bit winded trying to explain it in one breath. Did post in the projects section here on the furums and I just updated my hack-a-day projects page with what I did today. http://hackaday.io/project/1386-Neotype%3A-Haptic-Interfacing

2 Likes