bcd.h 548 B

1234567891011121314151617181920212223
  1. /* Permission is hereby granted to copy, modify and redistribute this code
  2. * in terms of the GNU Library General Public License, Version 2 or later,
  3. * at your option.
  4. */
  5. /* inline functions to translate to/from binary and binary-coded decimal
  6. * (frequently found in RTC chips).
  7. */
  8. #ifndef _BCD_H
  9. #define _BCD_H
  10. static inline unsigned int bcd2bin(unsigned int val)
  11. {
  12. return ((val) & 0x0f) + ((val & 0xff) >> 4) * 10;
  13. }
  14. static inline unsigned int bin2bcd(unsigned int val)
  15. {
  16. return (((val / 10) << 4) | (val % 10));
  17. }
  18. #endif /* _BCD_H */