# How can I perform binary operations?

## Recommended Posts

I'm searching a way to do xor and shift and if possible also other operations. Thanks in advance for the replies.

Edited by Dragonfighter
##### Share on other sites

Thanks but I'm looking for a way to know if the highest bit of a value is set. For example 224=‭1110 0000‬ that is different from ‭14‬= 0000 1110

Edited by Dragonfighter
##### Share on other sites
• Moderators
38 minutes ago, Dragonfighter said:

Thanks but I'm looking for a way to know if the highest bit of a value is set. For example 224=‭1110 0000‬ that is different from ‭14‬= 0000 1110

Notice how different this is from " I'm searching a way to do xor and shift and if possible also other operations."? Providing people with all the information they need helps us help you

How about a detailed explanation and example of what you would like to accomplish?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

##### Share on other sites

Sorry I was wrong. I'm looking for operation that can be done on binary and not on decimal values, for that I changed the question. The next time I'll provide more information. The script I'm doing is this:

```\$Binary=4
\$Binary1=10
;The goal of this operation is to control if the highest bit is set to 1 but for now doesn't contain anything
If Then
\$Result=BitXOR(\$Binary, \$Binary1)
Else
;In the case the highest bit isn't set to 1, it do a normal addition
\$Result=\$Binary+\$Binary1
EndIf```

That I'm making to avoid to create value that exceed 8 bit

Edited by Dragonfighter
##### Share on other sites
```Func _BitTest(\$value, \$bit)
Return BitAND(BitShift(\$value, \$bit),1)
EndFunc```

My Contributions and Wrappers

Spoiler

##### Share on other sites
11 minutes ago, Dragonfighter said:

;The goal of this operation is to control if the highest bit is set to 1

Highest bit is an ambiguous concept: it all depends which max bitsize your integers of interest are and if you have to handle negative values as well (and then how).

13 minutes ago, Dragonfighter said:

That I'm making to avoid to create value that exceed 8 bit

Easy: check whether \$Binary+\$Binary1> 255 or not

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites
4 hours ago, Dragonfighter said:

I'm looking for operation that can be done on binary and not on decimal values

They're the same thing, just represented differently. 1101 in binary is the same as 13 in decimal. So anything you want to do in binary can be done with decimal input, or vice versa.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

## Create an account

Register a new account

• ### Similar Content

• Func _Binary(\$Int) ;Uncomment To Only Accept Integers #cs If IsInt(\$Int) = 0 Then Return 0 EndIf #ce If \$Int < 0 Then ;Negative Numbers Will Break The Function Return 0000 EndIf Local \$Integer = \$Int Dim \$Bin[1] = [Mod(\$Integer, 2)] Local \$Counter = 1 Do \$Integer = Floor(\$Integer / 2) _ArrayAdd(\$Bin, Mod(\$Integer, 2)) Until \$Integer = 0 _ArrayReverse(\$Bin) ;Reverses The Array Because As Is, The Product Is Backwards ;A Loop To Remove Any Preceding 0's or Add 0's To Keep At Least Four Digits Select Case \$Int <= 1 \$Integer = "00" & _ArrayToString(\$Bin, "") Case \$Int = 2 Or \$Int = 3 \$Integer = "0" & _ArrayToString(\$Bin, "") Case \$Int >= 8 \$Integer = StringTrimLeft(_ArrayToString(\$Bin, ""), 1) Case Else \$Integer = _ArrayToString(\$Bin, "") EndSelect ;You Can Comment It Out Without Anything Else Having A Problem Return \$Integer EndFunc I made this because I was writing something that I could use to play with Bitwise Operations and using Binary() by itself wasn't helping.
It's very basic and will only take positive integers because that's all I needed but I'm sure with a little tweaking you could make it fit with negative and float types.
It returns a string essentially but doesn't pose a problem when just changing numbers into binary digits.
Example: If you were to do _Binary(5) you would get "0101" and _Binary(8) would return "1000"
Between this last sentence and here I've changed this about a half dozen times to refine it a bit because without it checking if your number is < 0 it would break if a negative number was inserted and it wouldn't even have a problem if you put in Float Values, Regular or Special Characters but that negative value will do the trick lol.
Anyway, I hope someone finds some use of this and thank you for reading!
-Pick

• Hi AutoIt Programmers, i wanna figure out how to use Binary functions in C# like:

BinaryMid
BinaryLen
IsBinary and other basic ones were too ez, but those two were hard to noob like me.

• By Stew
(Edited from original.  Please note that I AM NOT AN AUTOIT EXPERT.  I write code using Autoit frequently but I am no expert, especially when it comes to I/O.  So any remarks that start with "Why did you..." can be answered by referring to the first sentence.  This project was done in Autoit because of an interface I built to display the data.)
Attached is a program and ascii input file I wrote to read stock price data, convert it to binary and then read it back into the program in binary.  The goal was to show increased performance for reading the files in binary and provide a demo on how to read/write binary for int32, int64, double and strings for anyone who might find it helpful.  The results on my PC show the following:
Time to read ascii file only: 456.981951167202
Ascii read & process time: 6061.83075631701
Binary write file time: 14787.9184635239
Time just to read binary file: 42.418867292311
Binary read and process time: 4515.16129830537
A couple things to note:
1) The 32 MB ascii file took 10x longer to read than the 15 MB binary file.  Not entirely sure why.  Both were read into a buffer.
2) The Binary write takes a long time but I made no effort to optimize this because the plan was to write this file one time only so I don't mind if it takes longer to write this file.  I care much more about how long it takes to read the file because I will be reading it many times.
3) There was a modest gain in converting the ascii file to binary in terms of file size and reading speed.
So big picture... not sure it's worth the effort to convert the files to binary even though most of the data is numerical data in the binary file.  That was actually surprising as I expected there would be more of a difference.  Any ideas on how to get the binary data to read at a faster rate would be great.

binary.au3
2019_02_08.zip
• By FMS
Hello,
I'm trying to read a binary file to an array but couln't get it to work.
Also I coul not find any help in the forum around this subject whish was helpfull.
Is there any way it could be done?
I tried a lot of ways but maybe somebody know's the right way?
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local \$in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local \$data = FileRead(\$in) Local \$FileArray = BinaryToString(\$data,4) ;~ \$FileArray = StringSplit(\$BinarydData, @CRLF, 1+2) ;~ Local \$FileArray = StringRegExp(\$BinarydData, "[^\r\n]+", 3) FileClose(\$in) _ArrayDisplay(\$FileArray,"\$FileArray","",32) MsgBox(\$MB_SYSTEMMODAL, "", "\$FileArray = " & \$FileArray )
TEST_labels.idx1-ubyte
• By rudi
Hello.
I'm too stupid to see my mistake:
To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
The "TIFF Header Format" is easy:
Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
--> II
Offset 02h, 2 Byte = Version Nr.
Offset 04h, 4 Byte = pointer to first IFD entry

Howto read and analyse the binary content correctly? This is my messy, not operational code:

\$sampleTiff="H:\daten\tif\11\11\111111.TIF" \$h=FileOpen(\$sampleTiff,16) \$content=FileRead(\$h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$content = ' & \$content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose(\$h) \$type=VarGetType(\$content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$type = ' & \$type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console \$ToString=BinaryToString(\$content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$ToString = ' & \$ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) \$content=StringTrimLeft(\$content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$content = ' & \$content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for \$i = 1 to 8 step 8 \$next=StringMid(\$content,\$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$next = ' & \$next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console \$Chr=BinaryToString(\$next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$Chr = ' & \$Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
111111.TIF
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...