Posts: 3,616
Threads: 287
Joined: Jan 2003
Okay, the situation is I'm writing an encryption algo that uses string*1s, with XOR and NOT. My problem is that occasionally the algorithm results in the most-signifigant-bit (bit 7) being set. Which means that the number is negative.
The question is, is there a way to force an expression to treat bit 7 as any normal bit, not the sign-bit?
In C an expression that I want would be like this:
Code:
something1=(unsigned char) ~(something1 ^ something2);
Of course, ~ is NOT and ^ is XOR.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 1,272
Threads: 36
Joined: Feb 2003
Does this work?
Code:
something1= ~( (unsigned char)something1 ^ (unsigned char)something2 );
Posts: 3,616
Threads: 287
Joined: Jan 2003
Er...QB please.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 1,272
Threads: 36
Joined: Feb 2003
QB doesn't support signed chars. So there's just no way that QB could be treating bit 7 as a sign bit.
But here's the code:
Code:
something1 = CHR$(NOT (ASC(something1) XOR ASC(something2)))
Posts: 3,616
Threads: 287
Joined: Jan 2003
Oh, I think I know the problem, after you said that - I'm also using an array of singles in the expression...is there any way to use unsigned variables?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 1,272
Threads: 36
Joined: Feb 2003
Not in QB. THe best you can do is use the next larger variable type.
Posts: 3,616
Threads: 287
Joined: Jan 2003
*Screams*
Dammit! QB is *not* a language for quick encryption. Better move to C.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 3,279
Threads: 170
Joined: Nov 2003
You can easily trick QB into using unsigned integers. For a quick example of this, try using an INTEGER variable and a calculation to access anywhere in the video RAM of SCREEN 13 (like intValue = x + y * 320). In the QB IDE, you'll get an Overflow on any calculation that does this that exceeds 32767. However...compiled, it should work. Something about how BC compiles, some kind of bug I think. You can also cheat by representing values in hexadecimal notation. BC will seemingly ignore the sign. It's freakin' weird.
I'd knock on wood, but my desk is particle board.
Posts: 1,407
Threads: 117
Joined: Dec 2002
In fact if you use only logic operators, it does'nt matter if integers are signed or unsigned. That is important only if you use arithmetic
(for example if you use x2 to shift left and \2 to shift right). In that cases you must catch the cases where sign bit exists or changes. It's somewhat tricky....
You can print the resulting unsigned values by using :
Code:
a%=&hFFFF 'this is -1 signed
print A% and &hFFFF& 'it will print 65535
Antoni
Posts: 3,616
Threads: 287
Joined: Jan 2003
Ah. I've tried all that, it seems that QB simply doesn't support unsigned calculations.
*Moves to greener pasture...int main()*
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.