Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Forcing unsigned calculations
#1
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.
Reply
#2
Does this work?
Code:
something1= ~( (unsigned char)something1 ^ (unsigned char)something2 );
Reply
#3
Er...QB please. Smile
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#4
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)))
Reply
#5
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.
Reply
#6
Not in QB. THe best you can do is use the next larger variable type.
Reply
#7
*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.
Reply
#8
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.
Reply
#9
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
Reply
#10
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)