Thread: specific width values

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    specific width values

    Gotta go to work now so I'll just post the code with a TODO at the bottom explaining what I need to do,
    if any of ya got suggestions then please post 'em, either way I'll try again 2mw, 2nite I'll hafta go straight to sleep for early morning work 2mw
    Code:
    typedef struct _literaln {
    	char type[4];
    	size_t base;
    	uint_fast64_t width;
    	int_least64_t sig;
    	uint_least64_t num;
    	long double fpn;
    } LITERALN;
    ...
    void puti( FILE *file, LITERALN _literaln ) {
    	if ( _literaln.type[1] == U'u' ) {
    		switch (_literaln.type[2]) {
    		case U'l':
    			if ( _literaln.type[3] == U'l' )
    				fprintf( file, "(unsigned) %lluull", (ullong)(_literaln.num) );
    			else
    				fprintf( file, "(unsigned) %luul", (ulong)(_literaln.num) );
    			return;
    		case U'h':
    			if ( _literaln.type[3] == U'h' )
    				fprintf( file, "(unsigned) %hhuuhh", (uchar)(_literaln.num) );
    			else
    				fprintf( file, "(unsigned) %huuh", (ushort)(_literaln.num) );
    			return;
    		default:
    			if ( _literaln.type[2] == U'i' ) {
    				if ( _literaln.width != bitsof(int) )
    					fprintf( file, "(unsigned) %lluui%u",
    						(ullong)(_literaln.num), (uint)_literaln.width );
    				else
    					fprintf( file, "(unsigned) %uui", (uint)(_literaln.num) );
    			}
    			else
    				fprintf( file, "(unsigned) %uu", (uint)(_literaln.num) );
    			return;
    		}
    	}
    	else {
    		switch (_literaln.type[2]) {
    		case U'l':
    			if ( _literaln.type[3] == U'l' )
    				fprintf( file, "(signed) %lldll", (sllong)(_literaln.num) );
    			else
    				fprintf( file, "(signed) %ldl", (long)(_literaln.num) );
    			return;
    		case U'h':
    			if ( _literaln.type[3] == U'h' )
    				fprintf( file, "(signed) %hhdhh", (schar)(_literaln.num) );
    			else
    				fprintf( file, "(signed) %hdh", (short)(_literaln.num) );
    			return;
    		default:
    			if ( _literaln.type[2] == U'i' ) {
    				if ( _literaln.width != bitsof(int) )
    					break;
    				else
    					fprintf( file, "(signed) %di", (int)(_literaln.num) );
    			}
    			else
    				fprintf( file, "(signed) %d", (int)(_literaln.num) );
    			return;
    		}
    	}
    	// TODO: Interpret negative values correctly
    	fprintf( file, "(signed) %lldi%u",
    		(sllong)(_literaln.num), (uint)_literaln.width );
    }

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Well I thought of a method, for now this seems to work:
    Code:
    	bit = 1u << (_literaln.width - 1);
    	if ( _literaln.num & bit ) {
    		while ( (bit <<= 1) )
    			_literaln.num |= bit;
    	}
    I put it in place of that TODO and added the variable declaration at the top of the function

    Edit: There was a bug after all but that's only on positive numbers, I will sort that 2mw.
    Last edited by awsdert; 10-12-2019 at 03:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i assign pointers to specific array values?
    By brianjoo in forum C Programming
    Replies: 2
    Last Post: 11-29-2017, 04:15 AM
  2. Replies: 0
    Last Post: 04-20-2015, 11:07 AM
  3. Help! How to enter values in a specific format
    By enakta13 in forum C Programming
    Replies: 1
    Last Post: 09-22-2012, 11:45 PM
  4. picking out specific values from an array
    By jak9 in forum C Programming
    Replies: 7
    Last Post: 10-13-2010, 01:05 PM
  5. Restricting a type to a specific set of values...
    By DarkMasterBosel in forum C++ Programming
    Replies: 8
    Last Post: 01-01-2008, 11:16 PM

Tags for this Thread