Skip to content

Commit

Permalink
add bittwiddling64 test
Browse files Browse the repository at this point in the history
  • Loading branch information
edadma committed Apr 3, 2018
1 parent 612a826 commit 0d81f0f
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions tests/bittwiddling64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
void
out( char c ) {
*((char*) 0x20000) = c;
}

//#include <stdio.h>
//#define out( c ) putchar( c )

void
print( char* s ) {
while (*s)
out( *s++ );
}

void
println() {
out( '\n' );
}

char*
bin2str( long n, int radix, char* buf ) {
char digits[] = "0123456789ABCDEF";
char* p = &buf[33];
long quo = n;

if (n < 0)
quo = -quo;

*p-- = 0;

while (quo >= radix) {
*p-- = digits[(quo%radix)];
quo /= radix;
}

*p = digits[quo];

if (n < 0)
*--p = '-';

return p;
}

void
printn( long n ) {
char buf[34];
char* s = bin2str( n, 16, buf );

print( s );
}

long
abs( long x ) {
const long bit31 = x >> 63;

return (x ^ bit31) - bit31;
}

long
modifyBit( long x, unsigned char position, int newState ) {
long mask = 1 << position;
long state = newState;

return (x & ~mask) | (-state & mask);
}

long
flipBit( long x, unsigned char position ) {
long mask = 1 << position;

return x ^ mask;
}

long
isNegative( long n ) {
return (long)((unsigned long) n >> 63);
}

void
main() {
printn( abs(5) );
print( ", " );
printn( abs(0) );
print( ", " );
printn( abs(-5) );
println();

printn( modifyBit(0, 5, 0) );//0
print( ", " );
printn( modifyBit(0, 5, 1) );//0x20
print( ", " );
printn( modifyBit(0x77, 5, 0) );//0x57
print( ", " );
printn( modifyBit(0x77, 5, 1) );//0x77
println();

printn( flipBit(0, 5) );//20
print( ", " );
printn( flipBit(0x77, 5) );//57
println();

printn( isNegative(0) );
print( ", " );
printn( isNegative(-5) );
println();
}

0 comments on commit 0d81f0f

Please sign in to comment.