This kinda confirms my suspicion:
printf("%d \n", !100); // 0
printf("%d \n", !!100); // 1
printf("%d \n", !-100); // 0
printf("%d \n", !!-100); // 1
The not operator in C apperently can by used to convert a value to a
boolean, just a 0 or a 1.
However it also negates it sort of...
So this makes the double not operator !! kind of a funny way/trick to
convert it back again.
Only problem with it is it stays zero or positive and doesn't turn negative.
I also tested my other suspicion:
printf("%d \n", -8 >> 1); // prints -4
the SAR shift in action here keeps -8 negative when it's shifted down
towards -4.
A SHL would have turned this positive.
So basically a SAR shifts in one's from the most significant bit position
(towards least). * not entirely correct.
So basically a SHL shifts in zero's from the most significant bit position
(towards least).
So basically the C code produces:
(0 or 1) or (Negative).
However since it shifts all the way down to least significant bit, it will
turn into:
(0 or 1) or (0 or -1).
Though I am not exactly sure what happens if X is positive or X is zero when
using SAR.
I would expect:
0 SAR X to produce 0 ? or maybe not... since it will shift in 1's from the
msb side probably...
if true... if it's shift a left shifter with 1's than zero would turn
negative.
I'll test this... if this is not going to happen than the cpu does even more
processing for SAR:
Hmm interestingly enough:
printf("%d \n", 0 >> 1); // produces zero.
So my theory at * is not fully correct. eitherwise 0 should lead
to: -
2147483648 probably.
100000000andsoforth0000.
So CPU seems to be doing something extra for SAR.
Anyway this means final conclusion is indeed:
(0 or 1) or (0 or -1).
Not the strange thing is or-ing a positive value with a negative value...
that's kinda weird.
However maybe this final conclusion is wrong ?!
What does SAR 31 actually do ? hmmm...
What does it preserve ?
I would guess the highest significant bit ?
Let's that's that with a max negative value.
-
2147483648
printf("%d \n", -
2147483648 >> 31); // this leads to 1 as far as I can tell
which is kinda strange...
I'm a bit confused now...
One possibility is that this C function is actually bugged, and might
produce a wrong value for certain ranges.
Funny thing is I don't have a C test program available yet to test out all
ranges.
For now I will have to assume that this function could possibly be bugged !
;)
To the TEST CAVE BATMAN ! ;) =D
This will need to be continued... later... after verifieing is this function
is actually correct in C or if it has problems ?! ;)
TUTUTUTUTUTUTUTUTUTUTUTUTUTUT BATMAN !!!! PAAAAAWWWW !
Bye,
Skybuck.