Bitshifting in long datatype on Arduino UNO [SOLVED]

I am attempting to store 3 different bytes in a long datatype(i have my reasons), but i cannot seem to access any data past the 15th bit using bitwise operators. The long datatype is holding a full 32 bits. I was able to directly write and read 2^30ish just fine.

Here is a sample of the code and output:

long myLong=0 ;
void setup() {
  Serial.begin(9600);

  myLong=myLong | B1;
  myLong=myLong | B1<<8;
  myLong=myLong |B1 << 16 ;
}

  
void loop() {

  Serial.println(myLong, BIN );
  delay(1000);
}

the output is:
100000001

Is there a better way to do this?

What type is B1? What happens to that 16 bit type when you shift it 16 bits?

You also didn't tell us the value of B1.

Just a random guess, but I wonder if it's a precedence issue.

Try

myLong=myLong | (B1 << 8);

From the cores, binary.h:

#define B1 1

So it defaults to int, I guess.

Wouldn't it be easier to write "1" rather than "B1"?

B1 is just a binary number value 1.

So i just did bit shifting from 0 to 31 and something interesting happened:

Code:

unsigned long myLong=0 ;
void setup() {
  Serial.begin(9600);
}

  
void loop() {
for (int i = 0; i < 32;i++) {
  myLong=myLong | 1<<i;
  Serial.print(i);
  Serial.print(": ");
  Serial.println(myLong, BIN );

}
while(1)
{}
}

Output:

0: 1
1: 11
2: 111
3: 1111
4: 11111
5: 111111
6: 1111111
7: 11111111
8: 111111111
9: 1111111111
10: 11111111111
11: 111111111111
12: 1111111111111
13: 11111111111111
14: 111111111111111
15: 11111111111111111111111111111111
16: 11111111111111111111111111111111
17: 11111111111111111111111111111111
18: 11111111111111111111111111111111
19: 11111111111111111111111111111111
20: 11111111111111111111111111111111
21: 11111111111111111111111111111111
22: 11111111111111111111111111111111
23: 11111111111111111111111111111111
24: 11111111111111111111111111111111
25: 11111111111111111111111111111111
26: 11111111111111111111111111111111
27: 11111111111111111111111111111111
28: 11111111111111111111111111111111
29: 11111111111111111111111111111111
30: 11111111111111111111111111111111
31: 11111111111111111111111111111111

myLong=myLong | (B1 << 8);

Does bit shifting by a smiley face really help?

Does bit shifting by a smiley face really help?

Sorry... I've edited it . I skipped the code tags since it was only one line.

In fact bitwise or does have precidence over bit shift, so without the parenthesis the or will happen before the bit-shift.

How about this? (if you want something done once, do it in setup):

unsigned long myLong = 0;

void setup () 
  {
  Serial.begin (115200);

  for (int i = 0; i < 32; i++) 
    {
    myLong = myLong | (unsigned long) 1 << i;
    Serial.print (i);
    Serial.print (": ");
    Serial.println (myLong, BIN );
  }  // end of for loop
}  // end of setup

void loop() { }

Output:

0: 1
1: 11
2: 111
3: 1111
4: 11111
5: 111111
6: 1111111
7: 11111111
8: 111111111
9: 1111111111
10: 11111111111
11: 111111111111
12: 1111111111111
13: 11111111111111
14: 111111111111111
15: 1111111111111111
16: 11111111111111111
17: 111111111111111111
18: 1111111111111111111
19: 11111111111111111111
20: 111111111111111111111
21: 1111111111111111111111
22: 11111111111111111111111
23: 111111111111111111111111
24: 1111111111111111111111111
25: 11111111111111111111111111
26: 111111111111111111111111111
27: 1111111111111111111111111111
28: 11111111111111111111111111111
29: 111111111111111111111111111111
30: 1111111111111111111111111111111
31: 11111111111111111111111111111111

Nick,
After your first comment, it clicked for me and I was just implementing what you wrote.

Thanks a ton.

-Greg