|
-
Sep 17th, 2009, 04:09 PM
#1
[RESOLVED] Bits, Bytes and Boomerangs (without the boomerangs...)
Sorry, I like my thread titles with alliteration 
Anyway... now that you think I'm weird, lets get down to business.
As per this thread: http://www.vbforums.com/showthread.php?t=584412 I am trying to perform a DNS query in code. Having spent the last few hours trying to get my head around this and failing miserably I figured I would see if anyone on here could point me in the right direction.
The DNS RFC (http://www.faqs.org/rfcs/rfc1035.html) tells me that the header of each UDP packet that contains a DNS query should look like this:
Code:
The header contains the following fields:
1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
So we have got 16 bits (which I believe is 2 bytes) available for each section of the header, but as you can see the second section of the header is not just a nice full 2 byte value - its several individual bit values (with a couple of 4 bit values as well) and its this that is tripping me up at the moment.
I'm not great with Bytes, let alone Bits, so I'm pretty stuck with trying to work out how I can 'set' the relevant Bits and then combine the whole thing to make a Byte array that is 12 Bytes long.
I'm not even sure if I am setting the values correctly for the sections of the header that are 2 full bytes. Here is what I am doing at the moment, I'm using a List(Of Byte) with the intention of just calling ToArray when I have finished building it... because although the header is 12 bytes long, the rest of the packet has a variable length so I thought this would be easier than lots of ReDiming of a Byte array but feel free to tell me if thats a stupid idea 
vb.net Code:
Dim UDPClt As New UdpClient(53, AddressFamily.InterNetwork)
Dim QuestionBytes As New List(Of Byte)
'<--Construct Header-->
'ID
QuestionBytes.Add(10)
QuestionBytes.Add(10)
'Parameters
'-- This is the part that I need help with the most --
QuestionBytes.Add(some bits!)
'QDCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(1)
'ANCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(0)
'NSCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(0)
'ARCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(0)
'</--Construct Header-->
Is that even the right way to add bytes to a byte array - I mean can you add an item to a Byte array just using an Integer as the input or am I doing it wrong and I need to use some conversion? I'm surprised I didnt need to use CByte to keep Option Strict happy but maybe thats just because Bytes are still a bit of a mystery to me... (hopefully this will be a good learning experience).
I'm currently looking into the BitArray class but not getting anywhere fast and not even sure if thats what I should be doing so any pointers would be more than welcome!
Also, the other 2 byte sections in the header are defined as "An unsigned 16 bit Integer" but in the examples of DNS queries I have seen these fields are just set to 01 or 00 so can I get away with just writing two bytes as I am doing in my example code above? Does that equate to the same as a 16 bit unsigned integer?
Thanks 
Chris
-
Sep 17th, 2009, 04:28 PM
#2
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
You use division to move the bits you want to bit 0. Divide by a power of 2.
Then you use a mask to "only" allow your bits to come through.
4 bits is a nibble.
I'll work up a quick example for you.
-
Sep 17th, 2009, 04:31 PM
#3
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
 Originally Posted by szlamany
You use division to move the bits you want to bit 0. Divide by a power of 2.
Then you use a mask to "only" allow your bits to come through.
4 bits is a nibble.
I'll work up a quick example for you.
Is "a nibble" a good thing or a bad thing in this context? 
I also just noticed that one of the values in that second section is actually 3 bits (so there are two 4 bit values, one 3 bit value, and a few single bit values)
An example would be great thanks as I have no idea what you are talking about
-
Sep 17th, 2009, 04:43 PM
#4
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Ok - this is the first pass - how the value is built. Next post will show you how to extract it. OR is really used as a operator - that's binary addition.
Code:
Ok - let's say we got this pattern
|__|__ __ __|__ __ __ __| - this is my byte
0 1 2 3 4 5 6 7
1 2 4 8 16 32 64 128
I've broken it into a bit, then a 3-bit area, then that last 4-bit high order nibble.
And of course I want to store values in these areas - but the values are real numbers - starting at 1, right?
So the first bit - we will call that a flag - it's either on or off
The next 3 bits - they are a value that is 3 bits (1,2,4) and if I want to store 5 in that area I would put 101 (1 + 4 = 5).
The next 4 bits - another value - and lets say I want to store a decimal value of 10 in that area - that would be 0101 (2 + 8 = 10).
So in the long run I want to store this:
Code:
myByte = 1 (sets the first bit)
myByte = myByte OR (5 * 2)
' 5 is what I want to store - multiply it by 2 and it shifts to that location
myByte = myByte OR (10 * 16)
' 10 is what I want to store - multiply it by 16 and it shifts to that location
myByte should be equal to 1 + 10 + 160 which is 171 - right?
Now let's see what that 11010101 is equal to in decimal
Code:
1 1 0 1 0 1 0 1
1 2 8 32 128
1+2+8+32+128=171
-
Sep 17th, 2009, 04:52 PM
#5
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Ok - now this is really simple - let's show you how to get that middle 3-bit item out of the byte
First shift down - and 2 is the shift spot - that's what we multiplied by - remember?
my3Bit = myByte / 2
These have to be integer variables - so 171 / 2 (in an integer fashion) yields 85.
And of course 85 is
Code:
1 0 1 0 1 0 1
1+4+16+64=85
I've shifted my 3-bit and my nibble down - got rid of my first bit with that division.
Now I need to mask it
my3Bit = my3Bit AND 7
7 is 111
When you use the AND operator it only allow bits set in both integers to be seen
Code:
7 is 11100000
85 is 10101010
yields 10100000
The AND will only allow the 101 to come through - that's the use of the AND operator.
That mask we created of 111 (which is decimal 7) clears out the other bits in the resulting variable.
When you want to mask that nibble you use 1111 (which is decimal 15).
-
Sep 17th, 2009, 04:55 PM
#6
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Firstly, thanks Secondly, I didnt realise nibble was an actual technical term haha I thought you were just saying "thats a nibble" as in "that will be easy" or something... I dunno. Thirdly, I didnt understand half of that on the first read through so I will re-read it a few times and post any questions
-
Sep 17th, 2009, 05:15 PM
#7
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
OK I've read both posts 3 times now and I'm still a little confused (but getting there...)
The thing I'm struggling with is working out how you know that setting 101 in the 3 bit section will equal 5. From looking at the numbers you have written below each bit I would have thought 101 in that second section would equal either 1 + 3 or 2 + 8 (why are there 2 numbers beneath each bit by the way?)
Also, when you are just setting the entire Byte to 1 at the start - that means that setting a Byte to the Integer 1 and setting the first Bit of a Byte to 1 and the rest to 0 is the exact same thing yeah? but does it also mean that setting any Bit in the Byte to 1 is the same as just setting the Byte to 1? The reason I ask because the first bit field (I'm just referring to the second section of the DNS header in my original post) is defined as:
QR
A one bit field that specifies whether this message is a
query (0), or a response (1).
so that needs to be 0, as do most of the other bits for my query, but then this RD field (bit number 7) needs to be 1. So if I just add a Byte of 1 is that going to do the same thing as if I just set the seventh Bit of that Byte to 1? Hope that makes sense
-
Sep 17th, 2009, 05:19 PM
#8
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
OR sets a bit
AND is a mask for a bit
AND NOT clears a bit
101 is 5 because the bits are worth 1, 2, 4, 8 and so on.
101 is 1 + 4 - the 2 is missing.
You should really sit in the immediate window and do some
? 1 OR 4
? 5 AND NOT 4
? 5 * 2
and so on...
-
Sep 17th, 2009, 05:48 PM
#9
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
101 is 5 because the bits are worth 1, 2, 4, 8 and so on.
101 is 1 + 4 - the 2 is missing.
But when you set the second bit in the Byte to 1, how does it know you want the value 1 and not the value 2 - there isnt actually anything telling it that we are grouping those 3 bits together is there?
Lets modify your example a bit, split it up into different bit fields like so:
Code:
|__ __|__ __|__ __ __ __| - this is my byte
0 1 2 3 4 5 6 7
1 2 4 8 16 32 64 128
So now if we say that those same bits (bit 2, 3 and 4) are set to 1,0,1 respectively then does that mean they now produce a different value to your first example because they are in different 'sections'? I just dont understand where you are getting 1 + 4 from as the numbers you have written below the diagram do not place 1 and 4 under the 2nd and 4th bit
-
Sep 17th, 2009, 05:59 PM
#10
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
You broke it into a word, word and nibble. The first two words won't fit anything more then a 2-bit value.
If I want to store 5 in the high order nibble then I need to shift it up
5 in the low order nibble is
1010 0000
right? That's a 1 + 4.
If I want it up in the high order - looking like this
0000 1010
then I multiply the low-order 5 by the bit-value if that pivot spot - that's the 16 spot. 5 * 16 is 80 - right?
and of course
0000 1010
that 16 + 64 - that's 80!
I'll discuss your two low order words in the next post...
-
Sep 17th, 2009, 06:12 PM
#11
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Ok - those low order words you created can only store 2-bit values. And in two bits you can only store 0, 1, 2 or 3 (decimal) - 11 is 3, 01 is 2, 10 is 1 and 00 is 0 (just to show all combinations).
So I want to store a 2 in the first-low order word. And I want to store a 3 in the second low order word. And I want to store that 5 in the high order nibble.
That's going to look like
0111 1010 (decimal 94)
Do you see that?
2+4+8 along with 16+64 - for a total of decimal 94
Now to see a word - I first shift it to the down and then mask it with a WORD mask. A WORD mask is a 3 (11).
And of course to mask a nibble we use 15 (1111).
I'll do this in the immediate window:
Code:
myByte = 0
myByte = myByte OR (2) ' store a 2 in the low order
myByte = myByte OR (3 * 4) ' store a 3 in the next word (4 is the shift point)
myByte = myByte OR (5 * 16) ' store a 5 in the high order nibble
? mybyte
94
'it's really a 94 - built in the fashion I've just shown
?(mybyte \ 16) AND 15 ' I'm using \ to force integer division
' if you have 16 as an INTEGER constant the math will remain INTEGER
5
?(mybyte \ 4) AND 3
3
?(myByte) AND 3
2
You just saw me take the 94 in the myByte variable and do three different shifts with 2 different masks and you saw my original values of 5, 3 and 2.
-
Sep 18th, 2009, 05:07 AM
#12
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
OK I think I'm getting the hang of it, thanks for the example. I'll have a play around in the immediate window and see if I still have any questions
-
Sep 18th, 2009, 05:23 AM
#13
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Keep in mind that all this shifting is for looking at several bits from anywhere in the byte as if they were a real value living at bit 0.
Setting a bit or clearing a bit does not involve division - it's just using of OR or AND NOT.
Code:
myByte = myByte OR 4 ' this sets the bit at position 2
myByte = myByte AND NOT 4 ' this clears the bit as position 2
If you are going to do a lot of this create a constant for Bit$0 and Bit$1 and Bit$2 all the way up to Bit$15 or Bit$31 - whatever you want.
Beware of floating point math in all this - you don't want to accidently fall out of the integer datatypes.
-
Sep 18th, 2009, 06:06 AM
#14
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
OK thanks, while I still try to get my head around it and test things out can you just explain this to me:
In your example you show 2 numbers below each bit.
Code:
|__|__ __ __|__ __ __ __| - this is my byte
0 1 2 3 4 5 6 7
1 2 4 8 16 32 64 128
How does it know which number you wanted? Why are there 2 numbers below each one?
Actually looking at it again, were those top numbers (0 - 7) purely there for reference to show which bit the number below was referring to? I thought they were part of this whole thing... but if its just the bottom row of numbers I need to be aware of then that makes things a bit clearer
Thanks
-
Sep 18th, 2009, 06:12 AM
#15
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
 Originally Posted by chris128
In your example you show 2 numbers below each bit.
Code:
|__|__ __ __|__ __ __ __| - this is my byte
0 1 2 3 4 5 6 7
1 2 4 8 16 32 64 128
2 ^ 0 power is 1
2 ^ 1 power is 2
So Bit 0 is decimal value 1
and Bit 2 is decimal value 2
3 is 4
4 is 8
5 is 16...
2 ^ 7 power is 128
Back in my mainframe days we would have constants called Bit$0 which were equal to a 1 and Bit$1 equal to a 2 and Bit$2 equal to a 4.
But I can still recite powers of 2 from memory on demand!
-
Sep 18th, 2009, 06:53 AM
#16
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Yeah I get that but that is the bottom row of numbers, I am asking what the top row referes to (12345678)? Were you just using it to show which bits were which or are those numbers actually part of anything I need to know?
-
Sep 18th, 2009, 07:05 AM
#17
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Top row is 0, 1, 2, 3, 4, 5, 6 and 7
Bottom row is 1, 2, 4, 8...128
That is the "power" in the top row and the "decimal" value in the second row.
That's all - 2 ^ 0 is bit 0 and is decimal value 1
Just a way for me to label the byte - normal I would have put just 0, 1, 2...7
The real reason for that is to show low-order position vs high-order position. There are places that you will see the low on the left and some that put the high on the left.
-
Sep 18th, 2009, 07:21 AM
#18
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Just a way for me to label the byte
Ahhh I see! That was confusing me as I thought it was used in your explanation - although technically it is as the numbers below are the power of 2 of those top numbers like you said, but I only really need to know the bottom row as thats what dictates what each bit represents in decimal, right?
Also can you just clarify what you mean by high order and low order? Is it simply that the first 4 bits are low order and the last 4 bits are high order? You said that some people show them the other way round, if thats the case then how do you know which is which when its all just 1s and 0s ?
-
Sep 18th, 2009, 07:33 AM
#19
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
 Originally Posted by chris128
Ahhh I see! That was confusing me as I thought it was used in your explanation - although technically it is as the numbers below are the power of 2 of those top numbers like you said, but I only really need to know the bottom row as thats what dictates what each bit represents in decimal, right?
Exactly.
Even your example in the first post uses the 0, 1, 2 ... 15 as column labels. You have to somehow tell a person what bit is bit 0 and which is bit 15.
I added the row of 1,2,4,8 - the decimal row - to help you quickly understaned why I dividing by 16 or multiplying by 16 to shift something out and back into bit 4 position.
Also can you just clarify what you mean by high order and low order? Is it simply that the first 4 bits are low order and the last 4 bits are high order? You said that some people show them the other way round, if thats the case then how do you know which is which when its all just 1s and 0s ?
The 0 in the 0...15 (or 0...7) heading tells you the low order bit. You first need to know that to understand how they are orienting the key you are looking at.
When I refer to a high-order nibble when talking about a byte - since there are only two nibbles in a byte you know I'm talking about bits 4 to 7.
If I refer to the low-order word of a byte then I'm talking about 0 to 1. The high-order word of a byte is 6 to 7. No real way to use that lingo to talk about the word at 2 to 3. I guess that's the "high order word" in the "low order nibble" of a byte.
High and low really refer to left and right but since people notate bits with 0 on the left and others with 0 on the right I guess low and high became the best way to talk about which side of the byte we are discussing.
-
Sep 18th, 2009, 07:43 AM
#20
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
OK I think I get it, so just to confirm - this example from another website explaining bits:
With 8 bits in a byte, you can represent 256 values ranging from 0 to 255, as shown here:
0 = 00000000
1 = 00000001
2 = 00000010
...
254 = 11111110
255 = 11111111
They are doing it the other way round to your example yeah? high order first. Surely its important which order VB.NET uses though when you are working with bits in the way that I need to? Does it use high order first or low order? Or doesnt it matter..
-
Sep 18th, 2009, 07:49 AM
#21
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
 Originally Posted by chris128
They are doing it the other way round to your example yeah? high order first.
safer to say high order left - since we are really talking for your "visual indication". But yes - you are correct - they are using the more "traditional" method of show bit 0 - value 1 - as the first upper-right bit in the display.
Surely its important which order VB.NET uses though when you are working with bits in the way that I need to? Does it use high order first or low order? Or doesnt it matter..
It doesn't matter - this is all about visual representation on paper.
You in VB are using AND, AND NOT and OR with decimal values anyway - if you want to remove bit 0 you say
x = x AND NOT 1
or
x = x AND NOT (2 ^ 0) 'if you like to "see" the bit in the expression
or
x = x AND NOT BIT$0
and you define BIT$0 as a value of 1 in some module somewhere.
-
Sep 18th, 2009, 08:26 AM
#22
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
It doesn't matter - this is all about visual representation on paper.
I dont understand how it cant matter. Lets say I construct the following byte in VB:
If VB thinks that is high order left then that that means we have set bit 2 and all other bits are not set, so that means we have set a value of 4 yes?
If VB thinks that sequence is high order right (as in your original example) then we have set bit 5 so the value would be 32 wouldnt it?
-
Sep 18th, 2009, 08:49 AM
#23
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
OK so I had a stab at doing what I wanted to do originally - build this DNS header.
This is the table I am working with remember
Code:
The header contains the following fields:
1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
and this is my code now:
vb Code:
Dim HeaderBytes(11) As Byte
'--ID
HeaderBytes(0) = 55
HeaderBytes(1) = 65
'--Parameters
'First byte of parameter section
Dim FirstParametersByte As Byte = 0
FirstParametersByte = CByte(FirstParametersByte Or (1 * 128))
HeaderBytes(2) = FirstParametersByte
'Second byte of parameter section
HeaderBytes(3) = 0
'--QDCOUNT
HeaderBytes(4) = 0
HeaderBytes(5) = 0
'--ANCOUNT
HeaderBytes(6) = 0
HeaderBytes(7) = 0
'--NSCOUNT
HeaderBytes(8) = 0
HeaderBytes(9) = 0
'--ARCOUNT
HeaderBytes(10) = 0
HeaderBytes(11) = 0
What I am aiming for is for the RD bit in the second field (which I have posted in red font in the table above) to be set to 1, and the first field (the ID field) to be set to 5565 as this can apparently just be any 2 byte value. All other bits should be 0. Will that code I have got achieve that?
EDIT: Actually no sorry, I need to have the following in the QDCOUNT section:
an unsigned 16 bit integer specifying the number of
entries in the question section.
so as there is just one question can I just do this:
Code:
'--QDCOUNT
HeaderBytes(4) = 0
HeaderBytes(5) = 1
I get the feeling that wont work as I intend it to... I dont get how I can set a value of one across 2 bytes in any other way though (also, depending on how the byte array is ordered, couldnt that code be interpreted as 10 rather than 1?)
Last edited by chris128; Sep 18th, 2009 at 09:03 AM.
-
Sep 18th, 2009, 09:24 AM
#24
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
I found some C# code that does this same thing but I dont understand how it works and I dont want to just blindly follow it. Can you explain at all?
c#.net Code:
// the id of this message
data.Add((byte)0);
data.Add((byte)0);
// write the bitfields - _opCode is an Integer and _recursionDesired is a Boolean
data.Add((byte)(((byte)_opCode<<3) | (_recursionDesired?0x01:0)));
data.Add((byte)0);
// tell it how many questions
unchecked
{
data.Add((byte)(_questions.Count >> 8));
data.Add((byte)_questions.Count);
}
// etc etc, the other fields after this are just 0
From what I've read it looks like those >> and << are bit shifting operations. I'm still trying to get to grips with how to use those... but isnt this:
Code:
data.Add((byte)(_questions.Count >> 8));
the same as this:
because you are shifting every single set bit out of the byte by doing >> 8 arent you?
Last edited by chris128; Sep 18th, 2009 at 09:47 AM.
-
Sep 18th, 2009, 09:54 AM
#25
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
What's the data var?
If you have one byte, if you use the << 8 or the >> 8 (shif 8 bits), you have the original value...
And if you add to a byte another byte that has the value 0 you still got the same value...
10101010
+
00000000
-------------
10101010
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Sep 18th, 2009, 10:00 AM
#26
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
the data variable in their example is just an ArrayList.
I dont quite get what you are saying though... This _questions.Count value is obviously an Integer, so when you say doing >> 8 will get "the original value" what do you mean?
-
Sep 18th, 2009, 10:00 AM
#27
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
I think its in this case easier to stay in unsigned integers. The calculations for setting values over 2 bytes becomes easier. So why not using an array of UShort's
For setting a bit (set to 1) just use:
OrgVar = OrgVar OR bitweight (1,2,4,8,etc)
For reading if a bit is set:
TestVar = OrgVar AND bitweight (1,2,4,8,etc)
Test if TestVar > 0
Bitshifter are handy for setting or reading bits in a loop. But in this case I think you will not need it.
-
Sep 18th, 2009, 10:12 AM
#28
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Just to clear my point...
If you have One Byte (8 bits) shifthing 8 bits you will get the exact same value...
In the example, you have the count var, that's an integer (32bits) in .Net, so if you for shift right 8 bits it will be a different value...
Example:
0000 0000 0000 0000 0000 0001 0000 0000 = 512D
>> 8
0000 0000 0000 0000 0000 0000 0000 0001 = 1D
-----------
1000 0000 0000 0000 0000 0000 0000 0001 = 2 147 483 649D
>> 8
0000 0001 1000 0000 0000 0000 0000 0000 = 25 165 824D
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Sep 18th, 2009, 10:19 AM
#29
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Oh right it loops back round does it? I thought if a bit went past the end of the bit boundary then it was lost.
OK thats fine then, but I am now more confused about this - if I have an Integer value then how am I storing it in a single Byte (as an Integer is 4 Bytes long) ?
God this is confusing! 
For setting a bit (set to 1) just use:
OrgVar = OrgVar OR bitweight (1,2,4,8,etc)
I think thats what I am doing in this line where I am trying to set the "RD" bit flag isnt it?
Code:
FirstParametersByte = CByte(FirstParametersByte Or (1 * 128))
Can anyone actually tell me if that code I posted in post #23 is right or wrong for what I am trying to do?
-
Sep 18th, 2009, 10:20 AM
#30
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Sorry - don't know C myself - but it sure looks like << and >> are bit shift operators.
-
Sep 18th, 2009, 10:35 AM
#31
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
In this case bits 0 to 6 are reset and bit 7 is set:
Dim FirstParametersByte As Byte = 0 FirstParametersByte = CByte(FirstParametersByte Or (1 * 128)) HeaderBytes(2) = FirstParametersByte
What I am aiming for is for the RD bit in the second field (which I have posted in red font in the table above) to be set to 1, and the first field (the ID field) to be set to 5565 as this can apparently just be any 2 byte value. All other bits should be 0. Will that code I have got achieve that?
As far as I can see you are right.
HeaderBytes(4) = 0
HeaderBytes(5) = 1
I think it must be the other way arround:
HeaderBytes(4) = 1
HeaderBytes(5) = 0
-
Sep 18th, 2009, 10:47 AM
#32
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
I wrote something wrong...
I thought that in VB.NET the bit shifting was circular, but it isn't... 
 Originally Posted by MSDN
Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost (sign) bit is propagated into the bit positions vacated at the left. This means that if pattern has a negative value, the vacated positions are set to one; otherwise they are set to zero.
But it have a wired behavior because if you have a byte and if you shift by 8 bits you will get the exact same value, if you shift by 9 it's the same the shift by 1, that 8 shift that point me to the wrong direction...
About your question of the conversion of the Integer to Byte, i don't know how the C# handles the job, but if the number after the shifting can be saved in a byte there's no problem.
MSDN
Last edited by mickey_pt; Sep 18th, 2009 at 10:51 AM.
Reason: Link
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Sep 18th, 2009, 10:55 AM
#33
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Another question, i'm a little lost...
What do you want to know from this word (2bytes)?
QR| Opcode |AA|TC|RD|RA| Z | RCODE
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Sep 18th, 2009, 12:34 PM
#34
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
 Originally Posted by mickey_pt
Another question, i'm a little lost...
What do you want to know from this word (2bytes)?
QR| Opcode |AA|TC|RD|RA| Z | RCODE
I dont want to read from it, I want to be able to set the RD bit and leave all the other bits set to 0, which I'm hoping is what my code in post 23 does..
-
Sep 18th, 2009, 12:37 PM
#35
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Code:
Dim FirstParametersByte As Byte = 0
FirstParametersByte = CByte(FirstParametersByte Or (1 * 128))
Why are you using CBYTE() here? Are you stepping through? What is the value of FirstParametersByte?
btw - it's dangerous when working these bits to use a bit-size datatype that is tight. It's better to use a bigger integer datatype so that arithmatic overflows don't burn you (this is my experience from mainframe coding in this area).
-
Sep 18th, 2009, 12:40 PM
#36
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
Why are you using CBYTE() here? Are you stepping through? What is the value of FirstParametersByte?
You can see from that code you quoted what the value of FirstParametersByte is... its declared there and assigned 0 there. I'm using CByte because if I dont then it wont compile (Option Strict is on)
That FirstParametersByte is what I am using to create the first byte in this 2 byte part of the header:
Code:
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
because I want to set that RD field (which is 1 bit) and leave the rest of the bits as 0
-
Sep 18th, 2009, 12:45 PM
#37
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
You should use an INTEGER variable - it's got enough bits to work the entire 0 to 15 bit area.
Then convert that to whatever you need after - but at first you are doing simple bit manipulation that works well in an INTEGER variable
-
Sep 18th, 2009, 12:46 PM
#38
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
If you want to put the RD at 1 and everything else at 0...
Just put the first byte a 1 and the second a 0
HeaderBytes(2) = 1 (0000 0001)
HeaderBytes(3) = 0 (0000 0000)
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Sep 18th, 2009, 12:48 PM
#39
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
 Originally Posted by szlamany
You should use an INTEGER variable - it's got enough bits to work the entire 0 to 15 bit area.
Then convert that to whatever you need after - but at first you are doing simple bit manipulation that works well in an INTEGER variable
Huh? Why would I not use a Byte when I am working with bits and bytes
-
Sep 18th, 2009, 12:51 PM
#40
Re: Bits, Bytes and Boomerangs (without the boomerangs...)
I thought you were trying to manage 0 to 15 bits - which is two bytes - a word.
But you don't care if the variable has more bits available.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|