Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invert a number 1

Status
Not open for further replies.

nmath

Programmer
Dec 12, 2003
47
US
I am trying to invert a number and am running into problems.

For example: the number 248
in binary is 11111000
inverted should be 00000111

I have tried using the - operator to flip the bits and the NOT operator as well, both have not given me the results I need. If anyone has any ideas I would really appreciate it. Thanks in advance!
 
you'll need to StrReverse() it then convert it back to a int



___________________________________________________________________
[sub]
The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
[/sub]
 
Thats a great idea...the problem I am having is I need to have the number in binary first for this to work, as well as convert it back to decimal after it has been inverted. Is there a generic decimal to binary/binary to decimal function in ASP or am I going to need to write one? Also, how can I make it realize that the string is in binary and not in decimal?
 
good heavens!
onescomplement numbers.
I wrote a function in BASIC years ago while writing an assembler (relative jump calculations in machine code) and I will have the code archived. might take a while to find!

If memory serves you can multiply an integer by -1 and this will reverse the bit order, then use an iterative MOD 2 to convert to a binary string. (btw it probably exists in .net)

If no one beats me to it (most likely tarwn or onpnt) I'll find the code and post it up.



Chris.

Indifference will be the downfall of mankind, but who cares?
 
Hello nmath,

Without specifying the variable subtype, it is not a well-defined problem. So I assume the number is byte subtype. Then it is something like this for demonstration.
Code:
const bytemask=&HFF
x=248
on error resume next
y=bytemask xor cbyte(x)
if err.number<>0 then
    msgbox "Out of scope."
else
    msgbox y
end if
err.clear
on error goto 0
regards - tsuji
 
four functions to play with;
Binary returns an string of 8 bits,
BinToInt returns an Integer from an 8 bit binary string,
FlipBits does a logical inverse (1scomplement if you are into binary maths), btw (number xor 255) will also handle this.
pad will extend a binary string to 8 bits.

no real error checking for out of range values.

Code:
function Binary(varByte)
' return a binary string (8 bits) from a byte value
dim intRemain
dim strTemp
dim i

for i = 0 to 7 
intRemain = varByte mod 2
if intRemain > 0 then
	strTemp = "1" & strTemp
else
	strTemp = "0" & strTemp
end if
varByte = varByte \ 2
next
Binary = pad(strTemp)
end function

function BinToInt(binString)
' convert binary string to byte 
dim i
dim bit
dim intResult
binString = pad(binString)
bit = 7
for i = 1 to 8 
	if mid(binString,i,1) = "1" then
		intResult = intResult + (2 ^ bit)
		bit = bit - 1
	else
		bit = bit - 1	
	end if
next

BinToInt = cInt(intResult)
end function

function FlipBits(binString)
' inverse a string of bits 
dim strTemp
dim i

for i = 1 to len(binString)
	if Mid(binString,i,1) = "1" then
		strTemp = strTemp & "0"
	else
		strTemp = strTemp & "1"
	end if
next
FlipBits = strTemp
end function

function pad(binString)
' pad out to 8 bits 
dim i	
if len(binString) < 8 then
	for i = 0 to (7 - len(binString))
		binString = "0" & binString
	next
end if
pad = binString
end function

Enjoy! let me know if anything odd happens.



Chris.

Indifference will be the downfall of mankind, but who cares?
 
four functions to play with;
Binary returns an string of 8 bits,
BinToInt returns an Integer from an 8 bit binary string,
FlipBits does a logical inverse (1scomplement if you are into binary maths), btw (number xor 255) will also handle this.
pad will extend a binary string to 8 bits.

no real error done checking for out of range values.

Code:
function Binary(varByte)
' return a binary string (8 bits) from a byte value
dim intRemain
dim strTemp
dim i

for i = 0 to 7 
intRemain = varByte mod 2
if intRemain > 0 then
	strTemp = "1" & strTemp
else
	strTemp = "0" & strTemp
end if
varByte = varByte \ 2
next
Binary = pad(strTemp)
end function

function BinToInt(binString)
' convert binary string to byte 
dim i
dim bit
dim intResult
binString = pad(binString)
bit = 7
for i = 1 to 8 
	if mid(binString,i,1) = "1" then
		intResult = intResult + (2 ^ bit)
		bit = bit - 1
	else
		bit = bit - 1	
	end if
next

BinToInt = cInt(intResult)
end function

function FlipBits(binString)
' inverse a string of bits 
dim strTemp
dim i

for i = 1 to len(binString)
	if Mid(binString,i,1) = "1" then
		strTemp = strTemp & "0"
	else
		strTemp = strTemp & "1"
	end if
next
FlipBits = strTemp
end function

function pad(binString)
' pad out to 8 bits 
dim i	
if len(binString) < 8 then
	for i = 0 to (7 - len(binString))
		binString = "0" & binString
	next
end if
pad = binString
end function

Enjoy! let me know if anything odd happens.



Chris.

Indifference will be the downfall of mankind, but who cares?
 
Chris,

Thank you so much!! The functions work great!

-Nina
 
Could you not just AND the number with a string of zero's to get the result you need ?

to use your eg.
"For example: the number 248
in binary is 11111000
inverted should be 00000111"

11111000
00000000 &
--------
00000111
 
No, if you AND any number with 0 the result will be 0



Chris.

Indifference will be the downfall of mankind, but who cares?
 
Chris is right I tried it and it is always 0. I am not very good with this bitwise stuff. Thanks again Chris!!!! [bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top