Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: using uint64_t and mucking with bits...

  1. #1
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,533

    using uint64_t and mucking with bits...

    I am playing around with the usb host mouse code and was adding the ability to know which axis fields may have changed, so I was trying to return a 64 bit mask with a bit for each field that may have changed. With some of these joysticks I may have more than 32 fields so I tried using a uint64_t field.
    But I have run into some interesting results, that if I try to or in a 1<<n where n >= 32, all of the high 32 bits get set.

    Quick and dirty program that shows it:
    Code:
    void setup() {
    	while (!Serial) ;
    	Serial.begin(115200);
    	uint64_t mask = 0;
    
    	Serial.println("Set Bits");
    	for (uint i = 0; i < 64; i++ ) {
    		mask |= (1 << i);
    		Serial.printf("%u %x %x\n", i, (uint32_t)(mask >> 32), (uint32_t)(mask & 0xffffffff));
    	}
    
    	// Try shifting down...
    	Serial.println("\nShift bits down");
    	for (;mask; mask >>= 1) {
    		Serial.printf("%x %x\n", (uint32_t)(mask >> 32), (uint32_t)(mask & 0xffffffff));
    	}
    }
    
    void loop() {
    
    }
    As you can see from the output:
    Code:
    Set Bits
    0 0 1
    1 0 3
    2 0 7
    3 0 f
    4 0 1f
    5 0 3f
    6 0 7f
    7 0 ff
    8 0 1ff
    9 0 3ff
    10 0 7ff
    11 0 fff
    12 0 1fff
    13 0 3fff
    14 0 7fff
    15 0 ffff
    16 0 1ffff
    17 0 3ffff
    18 0 7ffff
    19 0 fffff
    20 0 1fffff
    21 0 3fffff
    22 0 7fffff
    23 0 ffffff
    24 0 1ffffff
    25 0 3ffffff
    26 0 7ffffff
    27 0 fffffff
    28 0 1fffffff
    29 0 3fffffff
    30 0 7fffffff
    31 ffffffff ffffffff
    32 ffffffff ffffffff
    33 ffffffff ffffffff
    34 ffffffff ffffffff
    35 ffffffff ffffffff
    36 ffffffff ffffffff
    37 ffffffff ffffffff
    38 ffffffff ffffffff
    39 ffffffff ffffffff
    40 ffffffff ffffffff
    41 ffffffff ffffffff
    42 ffffffff ffffffff
    43 ffffffff ffffffff
    44 ffffffff ffffffff
    45 ffffffff ffffffff
    46 ffffffff ffffffff
    47 ffffffff ffffffff
    48 ffffffff ffffffff
    49 ffffffff ffffffff
    50 ffffffff ffffffff
    51 ffffffff ffffffff
    52 ffffffff ffffffff
    53 ffffffff ffffffff
    54 ffffffff ffffffff
    55 ffffffff ffffffff
    56 ffffffff ffffffff
    57 ffffffff ffffffff
    58 ffffffff ffffffff
    59 ffffffff ffffffff
    60 ffffffff ffffffff
    61 ffffffff ffffffff
    62 ffffffff ffffffff
    63 ffffffff ffffffff
    
    Shift bits down
    ffffffff ffffffff
    7fffffff ffffffff
    3fffffff ffffffff
    1fffffff ffffffff
    fffffff ffffffff
    7ffffff ffffffff
    3ffffff ffffffff
    1ffffff ffffffff
    ffffff ffffffff
    7fffff ffffffff
    3fffff ffffffff
    1fffff ffffffff
    fffff ffffffff
    7ffff ffffffff
    3ffff ffffffff
    1ffff ffffffff
    ffff ffffffff
    7fff ffffffff
    3fff ffffffff
    1fff ffffffff
    fff ffffffff
    7ff ffffffff
    3ff ffffffff
    1ff ffffffff
    ff ffffffff
    7f ffffffff
    3f ffffffff
    1f ffffffff
    f ffffffff
    7 ffffffff
    3 ffffffff
    1 ffffffff
    0 ffffffff
    0 7fffffff
    0 3fffffff
    0 1fffffff
    0 fffffff
    0 7ffffff
    0 3ffffff
    0 1ffffff
    0 ffffff
    0 7fffff
    0 3fffff
    0 1fffff
    0 fffff
    0 7ffff
    0 3ffff
    0 1ffff
    0 ffff
    0 7fff
    0 3fff
    0 1fff
    0 fff
    0 7ff
    0 3ff
    0 1ff
    0 ff
    0 7f
    0 3f
    0 1f
    0 f
    0 7
    0 3
    0 1
    The second have of the code verified I can right shift down to use int the test code that simply tests the first bit to see if it is on...

    Now to find a different way.

    Should mention running on Windows 10, Arduino 1.9.0-bata31, TD 1.41 beta 2, T3.6 180mhz Serial

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    3,533
    Quick update: can fix with cast
    Code:
    		mask |= (uint64_t)((uint64_t)1 << i);

  3. #3
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,468
    yep, try mask |= (uint64_t)1 << i;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •