Format
C
Post date
2017-04-28 13:52
Publication Period
Unlimited
  1. /* Program to demonstrate the effect of LSB1st vs. MSB1st integers
  2. // by Joel Matthew Rees, Amagasaki, Japan
  3. // Copyright 2017 Joel Matthew Rees
  4. // All rights reserved.
  5. // Permission granted to use for personal purposes.
  6. // See http://defining-computers.blogspot.com/2017/04/lsb-1st-vs-msb-1st-ignore-gulliver.html
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. /* #define NO__DEPENDENCY_ON_LIMITS_H */
  11. #if !defined NO__DEPENDENCY_ON_LIMITS_H
  12. #include <limits.h>
  13. # define byteWidth ( (size_t) CHAR_BIT )
  14. # define byteMask ( (unsigned long) (unsigned char) ( (unsigned long) -1 ) )
  15. # define ulBytes ( sizeof (unsigned long) ) /* a run-time constant */
  16. #else
  17. unsigned byteWidth = 8; /* Not depending on limits.h . */
  18. unsigned long byteMask = 0xFF;
  19. unsigned ulBytes = 4; /* Sane throw-away initial values. */
  20. void setULbytes( void )
  21. { int i = 0;
  22. unsigned char chroller = 1;
  23. unsigned char chMask = 1;
  24. unsigned long ulroller = 1;
  25. while ( chroller != 0 )
  26. { chroller <<= 1;
  27. chMask = ( chMask << 1 ) | 1;
  28. ++i;
  29. }
  30. byteMask = chMask;
  31. byteWidth = i;
  32. i = 0;
  33. while ( ulroller != 0 )
  34. { ulroller <<= 1;
  35. ++i;
  36. }
  37. ulBytes = i / byteWidth;
  38. }
  39. #endif
  40. int putLSB( unsigned long ivalue, int early )
  41. { int i = 0;
  42. do
  43. { putchar( ivalue & byteMask );
  44. ++i;
  45. ivalue >>= 8;
  46. } while ( ( i < ulBytes ) && !( early && ( ivalue == 0 ) ) );
  47. return i;
  48. }
  49. int putMSB( unsigned long ivalue, int early )
  50. { int i = 0;
  51. do
  52. { putchar( ( ivalue >> ( ( ulBytes - 1 ) * byteWidth ) ) & byteMask );
  53. ++i;
  54. ivalue <<= byteWidth;
  55. } while ( ( i < ulBytes ) && !( early && ( ivalue == 0 ) ) );
  56. return i;
  57. }
  58. void fillch( int count, char ch )
  59. { while ( count-- > 0 )
  60. { putchar( ch );
  61. }
  62. }
  63. int printInteger( unsigned long ivalue, unsigned base )
  64. { char buffer[ 65 ];
  65. char * cpt = buffer + 65;
  66. * --cpt = '\0';
  67. if ( base > 36 )
  68. { base = 10;
  69. }
  70. do
  71. { int ch = ivalue % base;
  72. ivalue /= base;
  73. ch += '0';
  74. if ( ch > '9' )
  75. { ch += 'A' - '9' - 1;
  76. }
  77. * --cpt = ch;
  78. } while ( ivalue > 0 );
  79. fputs( cpt, stdout );
  80. return 64 - ( cpt - buffer );
  81. }
  82. int main( int argc, char *argv[] )
  83. {
  84. unsigned long my_integer = 123456;
  85. int index = 1;
  86. int length;
  87. #if defined NO__DEPENDENCY_ON_LIMITS_H
  88. setULbytes();
  89. #endif
  90. if ( argc > 1 )
  91. { char * endpt = argv[ 1 ];
  92. my_integer = strtoul( argv[ 1 ], &endpt, 0 );
  93. if ( endpt > argv[ 1 ] )
  94. { ++index;
  95. }
  96. else
  97. { my_integer = 123456;
  98. }
  99. }
  100. printf( "Data in memory: " );
  101. length = printInteger( my_integer, 10 );
  102. fillch( 32 - length, '\0' );
  103. length = printInteger( my_integer, 16 );
  104. fillch( 32 - length, '\0' );
  105. printf( "LSB1st early: " );
  106. length = putLSB( my_integer, 1 );
  107. fillch( 16 - length, '-' );
  108. printf( "LSB1st full: " );
  109. length = putLSB( my_integer, 0 );
  110. fillch( 16 - length, '-' );
  111. printf( "MSB1st early: " );
  112. length = putMSB( my_integer, 1 );
  113. fillch( 16 - length, '-' );
  114. printf( "MSB1st full: " );
  115. length = putMSB( my_integer, 0 );
  116. fillch( 16 - length, '-' );
  117. putchar( '\n' );
  118. return EXIT_SUCCESS;
  119. }
다운로드 Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text