What Does Shifting Variables OR REGISTERS means ? 1<<REGISTER_ADDRESS [closed] - operators

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have learned about shift left and shift right registers in bitwise operators. But i am confused with below code.
RX_DR shifted ? what does that mean ? this is just random example i just need to know meaning of it.
nrf24_configRegister(STATUS,(1<<RX_DR)|(1<<TX_DS)|(1<<MAX_RT));
Please help.

It looks like these constants are the bit numbers of various flags in a register. Looking at nRF24L01.h leads me to believe their values are:
#define RX_DR 6
#define TX_DS 5
#define MAX_RT 4
This corresponds to a bit layout like:
TX_DS
v
(bit #) 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
^ ^
(flag) RX_DR MAX_RT
Here I've labeled a few of the bits in an 8-bit register. Bit 6 is the RX_DR flag, bit 5 is the TX_DS flag, and bit 4 is the MAX_RT flag.
(1<<RX_DR)|(1<<TX_DS)|(1<<MAX_RT)
This bit of code constructs a register value where those three bits are set and the rest are unset. How does it do that? Consider 1<<TX_DS. This takes the value 1 (binary 00000001) and shifts it left 5 places, yielding 32 (binary 00100000).
That value is bitwise ORed (|) with the other two flags. ORing numbers together combines their 1 bits, yielding a value where all of the 1 bits from each operand are set.
1<<RX_DR == 1<<6 == 01000000 in binary
1<<TX_DS == 1<<5 == 00100000 in binary
1<<MAX_RT == 1<<4 == 00010000 in binary
ORing 1<<RX_DR and 1<<TX_DS and 1<<MAX_RT together gives the value 112, or in binary:
01110000
See how that binary value has bits 6, 5, and 4 set?

Related

Converting binary to decimal with out using a function

I'm trying to create a binary to decimal converter, and have got stuck on the code. I have researched forums for any help, but they all seam to use functions, which can not be used within a private sub. Please can anyone give me help on a solution to this problem?
I would use the positional notation method:
http://en.wikipedia.org/wiki/Positional_notation
http://www.wikihow.com/Convert-from-Binary-to-Decimal
So basically, without giving you the answer, you want to loop through binary place holders, filling up a variable as you go along. You would use an index to move from the least significant placeholder to the most.
For example : 10011011 in binary is 155 decimal.
So every place holder is a power with a base of two. Then you add the value for each one until your finished, like so:
placeholder 1 is: 2 pow 0 equals 1.
placeholder 2 is: 2 pow 1 equals 2.
placeholder 3 is: 2 pow 2 equals 4.
placeholder 4 is: 2 pow 3 equals 8.
placeholder 5 is: 2 pow 4 equals 16.
placeholder 6 is: 2 pow 5 equals 32.
placeholder 7 is: 2 pow 6 equals 64.
placeholder 8 is: 2 pow 7 equals 128.
Now we only add for the placeholders that have 1s.
128+16+8+2+1 = 155
What you will need:
A loop looping through indexes, and incrementing the exponent value as you go along, only adding the value if the index equals 1 in the binary number.
Hope my explanation makes sense. Good luck.

What does the Ampersand do in a SQL where clause [duplicate]

This question already has answers here:
What does "&" means in this sql where clause?
(5 answers)
Closed 9 years ago.
I'm trying to figure this query out and can't figure out how it relates:
Where iInteractionOpenReasonID & 16 = 0
Possible values for iInteractionOpenReasonID should be:
0 Unknown
1 Normal Start
2 Normal End
4 Transfer Start
8 Transfer End
16 Conference
32 Inter Queue
64 Networking
128 Segment
256 Compound
512 Block
1024 Clip Recording
I'm seeing values of 129 and 145 (which aren't in the list) and that where clause filters out the 145...I'm confused
"The & bitwise operator performs a bitwise logical AND between the two expressions, taking each corresponding bit for both expressions. The bits in the result are set to 1 if and only if both bits (for the current bit being resolved) in the input expressions have a value of 1; otherwise, the bit in the result is set to 0."
Try looking at the msdn doucmentation here. http://msdn.microsoft.com/en-us/library/ms174965.aspx

Convert String into digit in SQL [duplicate]

This question already has answers here:
Converting words to numbers in PHP
(6 answers)
Closed 8 years ago.
How to convert any string into digit in SQL without CASE and Decode function.
eg. THREE to 3
FOUR to 4
FIVE to 5
SIX to 6
Range is not decided.. can be vary upto N.
Well, I'm not sure whether this is what you need, but what about defining a table, say digits, like this:
digit: text | value: int
------------+-----------
one | 1
two | 2
three | 3
etc.
Then use a query, for example, like this one:
SELECT value FROM digits WHERE digit = 'FIVE'
Sure, it's pretty weird (to say the least), but nonetheless the use of CASE is avoided.

I'm new to visual basic and trying to understand how to set individual bits in a byte [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Hi, I am new to Visual Basic, I have a project where I need to be able to manipulate individual bits in a value.
I need to be able to switch these bits between 1 and 0 and combine multiple occurrences of bits into one variable in my code.
Each bit will represent a single TRUE / FALSE value, so I'm not looking for how to do a single TRUE / FALSE value in one variable, but rather multiple TRUE / FALSE values in one variable.
Can someone please explain to me how I can achieve this please.
Many thanks in advance.
Does it have to be exactly one bit?
Why don't you just use the actual built in VB data type of Boolean for this.
http://msdn.microsoft.com/en-us/library/wts33hb3(v=vs.80).aspx
It's sole reason for existence is so you can define variables that have 2 states, true or false.
Dim myVar As Boolean
myVar = True
myVar = Flase
if myVar = False Then
myVar = True
End If
UPDATE (1)
After reading through the various answers and comments from the OP I now understand what it is the OP is trying to achieve.
As others have said the smallest unit one can use in any of these languages is an 8 bit byte. There is simply no order of data type with a smaller bit size than this.
However, with a bit of creative thinking and a smattering of binary operations, you can refer to the contents of that byte as individual bits.
First however you need to understand the binary number system:
ALL numbers in binary are to the power of two, from right to left.
Each column is the double of it's predecessor, so:
1 becomes 2, 2 becomes 4, 4 becomes 8 and so on
looking at this purely in a binary number your columns would be labelled thus:
128 64 32 16 8 4 2 1 (Remember it's right to left)
this gives us the following:
The bit at position 1 = 1;
The bit at position 2 = 2;
The bit at position 3 = 4;
The bit at position 4 = 8;
and so on.
Using this method on the smallest data type you have (The byte) you can pack 8 bit's into one value. That is you could use one variable to hold 8 separate values of 1 or 0
So while you cannot go any smaller than a byte, you can still reduce memory consumption by packing 8 values into 1 variable.
How do you read and write the values?
Remember the column positions? well you can use something called Bit Shifting and Bit masks.
Bit Shifting is the process of using the
<<
and
>>
operators
A shifting operation takes as a parameter the number of columns to shift.
EG:
Dim byte myByte
myByte = 1 << 4
In this case the variable 'myByte' would become equal to 16, but you would have actually set bit position 5 to a 1, if we illustrate this, it will make better sense:
mybyte = 0 = 00000000 = 0
mybyte = 1 = 00000001 = 1
mybyte = 2 = 00000010 = (1 << 1)
mybyte = 4 = 00000100 = (1 << 2)
mybyte = 8 = 00001000 = (1 << 3)
mybyte = 16 = 00010000 = (1 << 4)
the 0 through to 16 if you note is equal to the right to left column values I mentioned above.
given what Iv'e just explained then, if you wanted to set bits 5, 4 and 1 to be equal to 1 and the rest to be 0, you could simply use:
mybyte = 25(16 + 8 + 1) = 00011001 = (1 << 4) + (1 << 3) + 1
to get your bits back out, into a singleton you just bit shift the other way
retrieved bit = mybyte >> 4 = 00000001
Now there is unfortunately however one small flaw with the bit shifting method.
by shifting back and forth you are highly likely to LOOSE information from any bits you might already have set, in order to prevent this from happening, it's better to combine your bit shifting operations with bit masks and boolean operations such as 'AND' & 'OR'
To understand what these do you first need to understand simple logic principles as follows:
AND
Output is one if both the A and B inputs are 1
Illustrating this graphically
A B | Output
-------------
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1
As you can see if a bit position in our input number is a 1 and the same position in our input number B is 1, then we will keep that position in our output number, otherwise we will discard the bit and set it to a 0, take the following example:
00011001 = Bits 5,4 and 1 are set
00010000 = Our mask ONLY has bit 5 set
if we perform
00011001 AND 0010000
we will get a result of
00010000
which we can then shift down by 5
00010000 >> 5 = 00000001 = 1
so by using AND we now have a way of checking an individual bit in our byte for a value of 1:
if ((mybyte AND 16) >> 1) = 1 then
'Bit one is set
else
'Bit one is NOT set
end if
by using different masks, with the different values of 2 in the right to left columns as shown previously, we can easily extract different singular values from our byte and treat them as a simple bit value.
Setting a byte is just as easy, except you perform the operation the opposite way using an 'OR'
OR
Output is one if either the A or B inputs are 1
Illustrating this graphically
A B | Output
-------------
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
eg:
00011001 OR 00000100 = 00011101
as you can see the bit at position 4 has been set.
To answer the fundamental question that started all this off however, you cannot use a data type in VB that has any resolution less than 1 byte, I suspect if you need absolute bit wise accuracy I'm guessing you must be writing either a compression algorithm or some kind of encryption system. :-)
01010100 01110010 01110101 01100101, is the string value of the word "TRUE"
What you want is to store the information in a boolean
Dim v As Boolean
v = True
v = False
or
If number = 84 Then ' 84 = 01010100 = T
v = True
End If
Other info
Technicaly you can't store anything in a bit, the smallest value is a char which is 8 bit. You'll need to learn how to do bitwise operation. Or use the BitArray class.
VB.NET (nor any other .NET language that I know of) has a "bit" data type. The smallest that you can use is a Byte. (Not a Char, they are two-bytes in size). So while you can read and convert a byte of value 84 into a byte with value 1 for true, and convert a byte of value 101 into a byte of value 0 for false, you are not saving any memory.
Now, if you have a small and fixed number of these flags, you CAN store several of them in one of the integer data types (in .NET the largest integer data type is 64 bits). Or if you have a large number of these flags you can use the BitArray class (which uses the same technique but backs it with an array so storage capacity is greater).

Determinant Finite Automata (JFLAP)

I have a DFA question (Determinant Finite Automata) . We are using JFLAP to construct the automata. I cannot figure this question out to save my life! Here it is
"DFA to recognize the language of all strings that have an even number of zeros and an odd number of ones."
So the alphabet is {0,1} and only using 0,1. So I need to build an automata that recognizes an even number of zeros and an odd number of ones.

			
				
I don't know whether my understanding is right.
I could give you the description in Grail format that generate an even number of zeros and an odd number of ones.
START 1
1 1 2
2 1 1
1 0 3
3 0 4
4 0 3
FINAL 3