Last active
April 9, 2019 18:31
-
-
Save justinmklam/f766526958eaa6e53acca70e75530572 to your computer and use it in GitHub Desktop.
Reference for bit shifting 32 bit to 8 bit and vice versa.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <EEPROM.h> | |
void EEPROMWrite32bit(uint8_t address, uint32_t value) | |
{ | |
uint8_t bytes[4]; | |
bytes[0] = (value >> 24) & 0xFF; | |
bytes[1] = (value >> 16) & 0xFF; | |
bytes[2] = (value >> 8) & 0xFF; | |
bytes[3] = value & 0xFF; | |
for (int i=0; i < 4; i++) | |
{ | |
EEPROM.write(address + i, bytes[i]); | |
} | |
} | |
uint32_t EEPROMRead32bit(uint8_t address) | |
{ | |
// Crucial point: Values are shifted in place, so variable needs to be able to contain the shifted value | |
uint32_t bytes[4]; | |
uint32_t value; | |
for (int i=0; i < 4; i++) | |
{ | |
bytes[i] = EEPROM.read(address + i); | |
} | |
value = (bytes[3] + (bytes[2] << 8) + (bytes[1] << 16) + (bytes[0] << 24)); | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment