G
Greg
Hi all,
I'm having a bit of problem with a piece of code I've written that I
think comes down to essentially an overflow, and I thought I'd check
with this group The code is included below, and I'd just like to say
before all the flames happen that i) I *know* it's extremely poor
programming style, but it is intended to be that hard to read, and ii)
that I don't think anyone really has to try and follow the code to
answer my question.
I'm still ducking though.
So, the problem is this: I'm using one element of my char array, let's
call it mask for clarity, as a bit mask. An ascii char value is then
bitwise anded with this. Am I right in thinking that when I leftshift
10000000 for a char that it rolls over (so to speak) and I get 11111111
back out? The behaviour of the programme seems to confirm this, but I'd
like an expert opinion
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke */
int bzs(char*c){
for(*++c= !(*++c = 1); (log(*c--) / log(2) < 9); *c-- += *c++ &
( *++c << 1 ), *c<<=1 ) ;
return (*++c&1 << (*c- * --c))-1;
}
int main( int argc, char **argv ) {
if( argc == 2 && (argv[0]=malloc(sizeof(char)*(strlen(argv[1])
+5))) ) {
FILE*f,* g;
if( sprintf( argv[0], "%s.bzs", argv[1] ), !(((f =
fopen( argv[1], "r" )) == NULL) || ((g=fopen( argv[0],"w"))==NULL) ))
while( fscanf(f, "%c",argv[0]) != EOF ) fprintf( g, "%d", bzs(argv[0])
);
}
return 0;
}
Incidentally, the code compiles cleanly
I'm having a bit of problem with a piece of code I've written that I
think comes down to essentially an overflow, and I thought I'd check
with this group The code is included below, and I'd just like to say
before all the flames happen that i) I *know* it's extremely poor
programming style, but it is intended to be that hard to read, and ii)
that I don't think anyone really has to try and follow the code to
answer my question.
I'm still ducking though.
So, the problem is this: I'm using one element of my char array, let's
call it mask for clarity, as a bit mask. An ascii char value is then
bitwise anded with this. Am I right in thinking that when I leftshift
10000000 for a char that it rolls over (so to speak) and I get 11111111
back out? The behaviour of the programme seems to confirm this, but I'd
like an expert opinion
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke */
int bzs(char*c){
for(*++c= !(*++c = 1); (log(*c--) / log(2) < 9); *c-- += *c++ &
( *++c << 1 ), *c<<=1 ) ;
return (*++c&1 << (*c- * --c))-1;
}
int main( int argc, char **argv ) {
if( argc == 2 && (argv[0]=malloc(sizeof(char)*(strlen(argv[1])
+5))) ) {
FILE*f,* g;
if( sprintf( argv[0], "%s.bzs", argv[1] ), !(((f =
fopen( argv[1], "r" )) == NULL) || ((g=fopen( argv[0],"w"))==NULL) ))
while( fscanf(f, "%c",argv[0]) != EOF ) fprintf( g, "%d", bzs(argv[0])
);
}
return 0;
}
Incidentally, the code compiles cleanly