## binair to bits

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

### binair to bits

Hey all

I need to know how I can get the bit (yeah the 0-1) from a binairy value.

Does anyone have such a convertor in Basic?

And incase you must now, it's to retrieve AH and AL from AX. If you have a better way, please let me know.

Grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

### Re: binair to bits

Seb McClouth wrote: from a binairy value.
What do you want to get binary values from?

A single, an integer, a string*1, etc.?

For example, I have input a character K\$. I would like a function like this:

PRINT BIT(k\$,3)

and I expect it to return 0 or 1 of bit 3.

Is that what you want?

Mac

burger2227
Veteran
Posts: 2408
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

### This Bit of my knowledge:

Binary bits are calculated in AH and AL as a total of 16 bits. The QB Integer byte value also is 2 bytes.

Code: Select all

``````                     AH                            AL
bit     (15) 14 13 12 11 10 9 8         7  6  5  4  3  2  1  0

Values      32767  to 256                      255 to 1
``````
Each is an exponent of 2 ^ bit so Bit0 = 2 ^ 0 = 1 etc.

So lets say bits 3 and 4 are set or on:

Code: Select all

``````bitset = 2 ^ 3 + 2 ^ 4 = 8 + 16 = 24        ' 00011000 in binary
``````
Bit 15 goes beyond the Integer allowance in QB and is normally not used as bits are defined as normal integers. This can be used to read or write port bit settings such as the 8 bit parallel data port. In fact even the pins are called D0 to D7.

The same values are found converting Binary numbers to decimal.

Now to see if a bit is on, just use the AND mathmatical function:

Code: Select all

``````IF TotalByteValue AND 16 THEN PRINT "Bit 4 is on"
``````
Using AND will return 16 also, so you can divide by 16 to get a 1 if needed, but it is not really necessary. You can do a total byte value up to 255 using a FOR i = 0 TO 7 and comparing the bits with 2 ^ i.

Ted
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:
Burger's way pretty much shows it in a nutshell. Pretty much, once you have the variable loaded, you can use one of the bitwise-operators to manipulate it.

Here's another example. Say that Bit 0 controls whether a certain action is done or not. You can toggle it many different ways.

Code: Select all

``````OurValue% = 1    'Bit 0 is TRUE

'To change Bit 0 to FALSE (0)
OurValue% = OurValue% AND 254   '11111110

'To toggle the value of Bit 0
OurValue% = OurValue% XOR 1   '00000001

To change Bit 0 to TRUE (1)
OurValue% = OurValue% OR 1``````

burger2227
Veteran
Posts: 2408
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

### Hum de dumb

It appears that Sebby has better things to do than reply to our posts.

So I won't bother with that dude no more!

Ted
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

BDZ
Coder
Posts: 49
Joined: Sun Nov 20, 2005 5:41 pm
Location: Wisconsin
Contact:

### Re: Hum de dumb

burger2227 wrote:It appears that Sebby has better things to do than reply to our posts.

So I won't bother with that dude no more!

Ted
Maybe you just answered his question so well he didn't want to insult your superior intellect by adding anything more to the thread.

I'm sure, though, that you'll find it in yourself to later stoop to his level and answer his questions, as you did so generously for Lee even after you promised not to help him anymore.

Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Contact:
Hey McTroll2227, I thought you didn't care about people saying "thank you"? Seems you can't keep your story straight.

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

### Re: Hum de dumb

burger2227 wrote:It appears that Sebby has better things to do than reply to our posts.

So I won't bother with that dude no more!

Ted
Hey Teddy

Soz about not replying but I'm extremly busing. I'm working full time.
I'm responsible for a family (that includes a wife and a baby girl). I also have to go to school for the job, one day a week.
The job involves irregulars hours so also irregular sleeping. And the irregular free time that I have I spend in working on QBinux

I did read the post and gave the codes a try.

If I don't reply doesn't mean I don't appreciate the work your guys doing.
Because I do and it helps a lot.

Thx for what your guys did and are doing this far!

Seb
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

burger2227
Veteran
Posts: 2408
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

### OK Seb

Teddy don't bother me sweetheart.......LOL. Thanks for acknowledging, but I see you here a lot and I got a lil pissy because I spent some time posting that. No problemo!

But Nod ought to stick with his crayons and quit messing with adults. Why don't you just stick to the FB forum and perhaps learn something? Make another childish game or something Nod. Your Avitar goes from STFU to "We are family" LOL. Grow up man! You gotta be over 18 by now.....

Yeah BDZ, you were right on that one. Seems when I get pissy here, good ole Nod comes around to pull my BLEEP.

Ted

PS: Are you BLIND NOD? Read the line below for cryin out loud!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:
I understand. Nothing more fustrating then ppl who you do a lot for, never say thx or anything.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

### Thanks! I'll try your information tomorrow.

Seb, once someone answers your post, all you have to do, if you read it, is put in a very short "Thank you" note, stating your intention of answering at a later time or date. This is called "acknowledging". This is the minimum respect one shows for others, and there is really no good excuse for not acknowledging anyone who helps. It's just a matter of self-conviction or of self-trainging, and, it only takes less than a minute to do.

The above is meant only as "an instruction for better living", and is not meant to offend. Hope you understand.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:
That true. Thx Ralph.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Contact:
burger2227, go back to sucking face with Pete at qbasic.com. The two of you make a cute couple, or so they say anyways.

burger2227
Veteran
Posts: 2408
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

### AHA

So we got you visiting too! GREAT, welcome aboard. Don't be shy........

Ted
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:
Hey guys

Due to some Linux stuff I was again in need of the binary to bit.
It was for the ~ (complementary stuff).
If you don't know what this is:
e.g. we have 8 in 4 bit => 1000, when doing the ~action, it becomes 0111, because it simple changes 1 to 0 and 0 to 1.

Maybe you already know how to do this but here's the code I came up with. I know it looks very noobish but atleast it seems to work. And the fact that I choose for a certain amount of bugs might not be correct.

Code: Select all

``````temp1 = 500 'we gonna complementair 500 in this example.

temp\$ = ""
temp2 = 0
mirrortemp\$ = ""
temp3 = 0
compltemp\$ = ""
mirrorcompltemp\$ =""
temp4 = 0

FOR a = 0 TO 20  'I'm assuming for the test we only need 20 bits
If temp1 AND 2^a THEN 'as Ted told me to test, I'm testing here if it goes
temp\$ = "1"+ temp\$ 'Why did I assume again this has to be one?
temp2 = temp2 + (temp1 AND 2 ^ a) 'to test if temp2 = temp1 then
IF temp2 = temp1 THEN EXIT FOr        'we leave the for-next-thing
ELSE
temp\$ = "0" + temp\$
END IF
NEXT

'The above code gives us the bits for temp1

FOR a = 1 to len (temp\$)
mirrortemp\$ = MID\$(temp\$, a, 1) + mirrortemp\$  'mirror the bit from temp\$ for check
NEXT

'from here I'm lost in remarks, but it works.

FOR a = 1 TO LEN(mirrortemp\$)
IF MID\$(mirrortemp\$, a, 1) = "1" THEN
IF a = 1 THEN
temp3 = temp3 + 1
ELSE
temp3 = temp3 + 2 ^ (a-1)
END IF
END IF
NEXT

IF temp3 = temp1 THEN 'compare temp3 and temp1
FOR a = 1 TO LEN(temp\$)
IF MID\$(temp\$, a, 1) = "0" THEN
compltemp\$ = compltemp\$ + "1"
ELSEIF MID\$(temp\$, a, 1) = "1" THEN
compltemp\$ = compltemp\$ + "0"
END IF
NEXT

FOR A = 1 to LEN(compltemp\$)
mirrorcompltemp\$ = mid\$(compltemp\$, a, 1) + mirrorcompltemp\$
NEXT

FOR a = 1 to LEN(mirrorcompltemp\$)
IF MID\$(mirrorcompltemp\$, a, 1) = "1" THEN
IF a = 1 THEN
temp4 = temp4 + 1
ELSE
temp4 = temp4 + 2 ^ (a-1)
END IF
END
NEXT
``````
So, is there an easier way? Or should this do the trick.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:
If you're only dealing with 4 bits, then a simple

Code: Select all

``YourValue% XOR 16``
should give you what you're looking for. That's saying if you pull out the 4 bits you needed into the YourValue%, that is.

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:
Well I need the code for a value which is calculated with several things I don't know the outcome. So basically I made a calculation (see code under here)

Code: Select all

``````FOR a = 0 TO 20  'I'm assuming for the test we only need 20 bits
If temp1 AND 2^a THEN 'as Ted told me to test, I'm testing here if it goes
temp\$ = "1"+ temp\$ 'Why did I assume again this has to be one?
temp2 = temp2 + (temp1 AND 2 ^ a) 'to test if temp2 = temp1 then
IF temp2 = temp1 THEN EXIT FOR        'we leave the for-next-thing
ELSE
temp\$ = "0" + temp\$
END IF
NEXT
``````
simple exits the for-next if the calculated bits are the same as the input. So if we have 500 for example we have 9 bits, 200 for example 8 bits, if I'm not mistaken.

I'm not sure weither I could use XOR for that matter. But I do now understand now 32 bits = 4294967296, etc.

That's for that insight.

But I'll keep your code in mind, might need it for someother thing in QBinux. Thx Patz.
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

burger2227
Veteran
Posts: 2408
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

### Conversions

Below converts a decimal number to binary. It uses Integer division and mod with the base 2 system of binary:

Code: Select all

``````SUB Dec2Bin
COLOR 14: LOCATE 2, 30: PRINT "Decimal to Binary"
COLOR 10: LOCATE 5, 20: INPUT "Enter a decimal number: ", num&
DO
remain = num& MOD 2    'remainder is used for binary result
num& = num& \ 2           'discard any remainder with integer division
Bin\$ = LTRIM\$(STR\$(remain)) 'make string number
Binary\$ = Bin\$ + Binary\$    'add remainder to binary number
LOOP UNTIL num& = 0
COLOR 11: LOCATE 10, 30: PRINT "Binary number = "; Binary\$  'binary result

END SUB
``````
You can convert decimal values to any other base number system using the base number. The binary result shows which bits are on as 1's.

Ted
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:
Thx Man!!

I'm gonna try the code later. But it looks just like the thing I need!!
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.

Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:
Not entirely related, but...

http://www.petesqbsite.com/forum/viewtopic.php?t=1830

Also, a more simple but less universal (than the one in the other post I linked to) code to determine the bits of a number...

Code: Select all

``````'Inputs: YourNum% - The number to turn into binary.
'Outputs: Bin\$ - The number in binary, formatted as a string.

DO
MaxBit% = MaxBit% + 1
LOOP UNTIL 2 ^ MaxBit% > YourNum%

'Remove the part above and replace the MaxBit% below if you already
'know the highest bit your program will have, or if you can only take
'certain bits (ie, up to 16)

FOR A = 0 TO MaxBit%
Bin\$ = LTRIM\$(RTRIM\$(STR\$(ABS(((2 ^ A) AND YourNum%) > 0)))) + Bin\$
NEXT A``````
Last edited by Patz QuickBASIC Creations on Thu Feb 07, 2008 5:53 pm, edited 1 time in total.