- /* Program to demonstrate the effect of LSB1st vs. MSB1st integers
- // by Joel Matthew Rees, Amagasaki, Japan
- // Copyright 2017 Joel Matthew Rees
- // All rights reserved.
- // Permission granted to use for personal purposes.
- // See http://defining-computers.blogspot.com/2017/04/lsb-1st-vs-msb-1st-ignore-gulliver.html
- */
- #include <stdio.h>
- #include <stdlib.h>
- /* #define NO__DEPENDENCY_ON_LIMITS_H */
- #if !defined NO__DEPENDENCY_ON_LIMITS_H
- #include <limits.h>
- # define byteWidth ( (size_t) CHAR_BIT )
- # define byteMask ( (unsigned long) (unsigned char) ( (unsigned long) -1 ) )
- # define ulBytes ( sizeof (unsigned long) ) /* a run-time constant */
- #else
- unsigned byteWidth = 8; /* Not depending on limits.h . */
- unsigned long byteMask = 0xFF;
- unsigned ulBytes = 4; /* Sane throw-away initial values. */
- void setULbytes( void )
- { int i = 0;
- unsigned char chroller = 1;
- unsigned char chMask = 1;
- unsigned long ulroller = 1;
- while ( chroller != 0 )
- { chroller <<= 1;
- chMask = ( chMask << 1 ) | 1;
- ++i;
- }
- byteMask = chMask;
- byteWidth = i;
- i = 0;
- while ( ulroller != 0 )
- { ulroller <<= 1;
- ++i;
- }
- ulBytes = i / byteWidth;
- }
- #endif
- int putLSB( unsigned long ivalue, int early )
- { int i = 0;
- do
- { putchar( ivalue & byteMask );
- ++i;
- ivalue >>= 8;
- } while ( ( i < ulBytes ) && !( early && ( ivalue == 0 ) ) );
- return i;
- }
- int putMSB( unsigned long ivalue, int early )
- { int i = 0;
- do
- { putchar( ( ivalue >> ( ( ulBytes - 1 ) * byteWidth ) ) & byteMask );
- ++i;
- ivalue <<= byteWidth;
- } while ( ( i < ulBytes ) && !( early && ( ivalue == 0 ) ) );
- return i;
- }
- void fillch( int count, char ch )
- { while ( count-- > 0 )
- { putchar( ch );
- }
- }
- int printInteger( unsigned long ivalue, unsigned base )
- { char buffer[ 65 ];
- char * cpt = buffer + 65;
- * --cpt = '\0';
- if ( base > 36 )
- { base = 10;
- }
- do
- { int ch = ivalue % base;
- ivalue /= base;
- ch += '0';
- if ( ch > '9' )
- { ch += 'A' - '9' - 1;
- }
- * --cpt = ch;
- } while ( ivalue > 0 );
- fputs( cpt, stdout );
- return 64 - ( cpt - buffer );
- }
- int main( int argc, char *argv[] )
- {
- unsigned long my_integer = 123456;
- int index = 1;
- int length;
- #if defined NO__DEPENDENCY_ON_LIMITS_H
- setULbytes();
- #endif
- if ( argc > 1 )
- { char * endpt = argv[ 1 ];
- my_integer = strtoul( argv[ 1 ], &endpt, 0 );
- if ( endpt > argv[ 1 ] )
- { ++index;
- }
- else
- { my_integer = 123456;
- }
- }
- printf( "Data in memory: " );
- length = printInteger( my_integer, 10 );
- fillch( 32 - length, '\0' );
- length = printInteger( my_integer, 16 );
- fillch( 32 - length, '\0' );
- printf( "LSB1st early: " );
- length = putLSB( my_integer, 1 );
- fillch( 16 - length, '-' );
- printf( "LSB1st full: " );
- length = putLSB( my_integer, 0 );
- fillch( 16 - length, '-' );
- printf( "MSB1st early: " );
- length = putMSB( my_integer, 1 );
- fillch( 16 - length, '-' );
- printf( "MSB1st full: " );
- length = putMSB( my_integer, 0 );
- fillch( 16 - length, '-' );
- putchar( '\n' );
- return EXIT_SUCCESS;
- }