Working with Bits in VBA

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Hello,
I have two questions relating to the sizes of variables in VBA.

I've been just looking at Ruddle's code in this thread (Algorithm Question)
http://www.mrexcel.com/forum/showthread.php?t=566351

and I started to wonder if we could work with bits in VBA in any ways just like from C++?
http://www.cplusplus.com/reference/stl/vector/


Moreover, this led to a new question which I had not known until now.
Why does a boolean take up 2 BYTES!!!! in VBA?
as stated in http://www.ozgrid.com/VBA/variables.htm

Thank you in advance,
Kpark.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
My stab at #3 is because a boolean is either -1 or 0. Since byte only goes from 0 - 255, it can't hold the negative number.

Why it's a -1 and 0 rather than a 0 and 1, I have no idea.
 
Upvote 0
You can certainly represent positive and negative numbers in eight bits, employing the MSB as the 'negativity indicator', so the range of values would be -128 (10000000) to 127 (01111111). The value of a unit of storage isn't some absolute thing, it's how the language interprets the bits it's given.

The reason a Boolean occupies two bytes rather than just a single bit (which would be sufficient) is, I suspect, because two bytes is the minimum amount of memory a 16-bit processor can fetch, and unless there's something else which is happy to use the spare 15 bits that the Boolean doesn't have any need of, those bits aren't going to be used anyway, so rather than devote any processor power to slicing the two bytes up, VBA just uses them all.
 
Upvote 0
I've been just looking at Ruddle's code in this thread (Algorithm Question)
http://www.mrexcel.com/forum/showthread.php?t=566351
That's a horrible piece of code, particularly the way I do maths on binary numbers by handling them as strings of "0" and "1" characters.

I fully had it in mind to find a quicker algorithm but as usual, other things come along which have a higher priority. I shall have to look for a more efficient way of bit-twiddling as soon as I can.

Well... not tonight because I'm going out... and tomorrow we have visitors coming round... :(
 
Upvote 0
Yes, a Byte data type can represent 0-255 because it's unsigned; an Integer daya type represents -32768 to 32767 because it's signed. That's a good article.
Stuff I will be learning next year...
What are you doing next year?

Although I don't wave it around (unless I'm touting for work), I have a degree in Computer Science dating back to 1983 - when these things were regarded with awe and amazement by most people - and we spent some time 'bit-twiddling' in various flavours of assembler.
 
Upvote 0
That's a horrible piece of code, particularly the way I do maths on binary numbers by handling them as strings of "0" and "1" characters.

I fully had it in mind to find a quicker algorithm but as usual, other things come along which have a higher priority. I shall have to look for a more efficient way of bit-twiddling as soon as I can.

Well... not tonight because I'm going out... and tomorrow we have visitors coming round... :(

Haha. I've been looking at the code. I was hoping to help optimize it as well but I couldn't find anyway of dealing with it except for strings in VBA
which I assume each character has the same size as a BYTE (is it because of an ascii table?)


mikerickson said:
VBA will allow bitwise operations on numbers

4 Or 1 = 5
3 Or 2 = 3
3 And 1 = 1

Ahh I see.
I was more hoping for allocating memory at the level of bits!
Thank you anyways for the reply! :D

Ruddles said:
What are you doing next year?

Although I don't wave it around (unless I'm touting for work), I have a degree in Computer Science dating back to 1983 - when these things were regarded with awe and amazement by most people - and we spent some time 'bit-twiddling' in various flavours of assembler

I'm taking Electrical & Computer engineering for my 2nd year in university.
I got into C programming last year in a different engineering program and I absolutely loved it.
So, I decided to transfer to electrical & computer engineering in my 2nd year after heavily debating whether I should go into computer science or engineering.

I really respect all the programmers and I want to become better in programming as well! So, I joined this forum to meet programming gurus and veterans like you and all the MVPs :D

There are so many things I still don't know and hope to gain alot more in the future.

I'm so afraid of learning assembly language. I heard it was maddd hard O_o
I'll be learning something called Verilog, I think?
Haven't done much research on it tho.
 
Upvote 0
Ahh I see.
I was more hoping for allocating memory at the level of bits!
Thank you anyways for the reply! :D
If you are looking for something like BASIC's POKE and PEEK commands, Mac can't do that. Are you on a Windows machine? If so, I've got a link (at home) that might help you.

Or are you looking for VBA commands that work at the level of an assembly language? (sorry, no)
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,317
Members
452,905
Latest member
deadwings

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top