Click here to Skip to main content
15,894,646 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problems with SQL database connection string Pin
Steve McLenithan7-Jan-03 4:48
Steve McLenithan7-Jan-03 4:48 
GeneralIntPtr to a structure Pin
Nnamdi Onyeyiri6-Jan-03 6:32
Nnamdi Onyeyiri6-Jan-03 6:32 
GeneralRe: IntPtr to a structure Pin
leppie6-Jan-03 7:32
leppie6-Jan-03 7:32 
GeneralRe: IntPtr to a structure Pin
Nnamdi Onyeyiri6-Jan-03 7:34
Nnamdi Onyeyiri6-Jan-03 7:34 
GeneralRe: IntPtr to a structure Pin
leppie6-Jan-03 7:55
leppie6-Jan-03 7:55 
GeneralRe: IntPtr to a structure Pin
Nnamdi Onyeyiri6-Jan-03 7:58
Nnamdi Onyeyiri6-Jan-03 7:58 
GeneralRe: IntPtr to a structure Pin
leppie6-Jan-03 8:04
leppie6-Jan-03 8:04 
GeneralRe: IntPtr to a structure Pin
James T. Johnson6-Jan-03 14:49
James T. Johnson6-Jan-03 14:49 
Understanding how to use bitshifts, etc to me seems to be something you either get or you don't. I'll do my best to try to explain it.

In most case you're referring to a 32-bit integer, I lay them out in my head like so

(31) -------- -------- -------- -------- (0)

bit 0 is on the right, and bit 31 on the left.

Bitwise operators work by using a truth table to get the result of doing an operation on two bits.

And (&) | 0 | 1 |   Or (|) | 0 | 1 |   XOr (^) | 0 | 1 |
--------|---|---|   -------|---|---|   --------|---|---|
    0   | 0 | 0 |      0   | 0 | 1 |       0   | 0 | 1 |
--------|---|---|   -------|---|---|   --------|---|---|
    1   | 0 | 1 |      1   | 1 | 1 |       1   | 1 | 0 |
Now that we have the truth tables we can almost begin to construct equations to use them.

To use them we need to figure out what values we need to use in order to get the appropriate values out.

ex. We want to check bit 15 of some 32-bit integer.

To check certain bits you want to isolate them, so the easiest way to accomplish this is to AND the value with a mask so that only the value of bit 15 comes out in the end.

Looking at the truth table for AND above, we see that anything ANDed with 0 results in zero, but if you AND something with 1 you get the orignal value out. BINGO!

So, our mask looks like:
(31) 00000000 00000000 10000000 00000000 (0)

if you AND the above with any value, you'll get out the value of bit 15.

This mask is easily generated, its simply (1 << 15) (1 shifted 15 places to the left, (1 << 3) is 00001000, 1 shifted 3 places to the left).

Typically you'll see the bitmasks as flags in an enum (or when doing interop as #define's). The values will often be represented in hex, because its generally easier to do some of these in your head.

Counting in hex... 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200... Those are the values from bit 0 to bit 9.

Now lets say that bits 8 through 23 represent something and you want that value.

First, you need to mask out bits 0-7 and 24-31:

The mask is simple
(31) 00000000 11111111 11111111 00000000 (0)
Now convert each section of 8 bits to hex, and we get 0x00FFFF00

Now AND the original value with the mask.

int val = (original & 0x00FFFF00);

You can't use the original value as-is, its still too large because you still have some extra numbers at the end! To sovle that problem, SHIFT the value to the right however many bits you need. In this case, 15 bits to the right.

val = (val >> 15);

Now val contains the numeric value contained in bits 8-23 of the original value.

Sometimes you need to OR (|) two values together to create a mask, typically you do this when you are only interesting in whether one or more of those bits are set, not WHICH of those bits is set.

ex. If bit 2 is set, then a key has been pressed and released.
If bit 3 is set, then a key is being held down.

Determine if a key has been pressed.

First generate the two masks we need
(7) 00000010 (0) - bit 2
(7) 00000100 (0) - bit 3

Now OR them together to get the mask to tell you if either of the bits are set.

(7) 00000110 (0) - bits 2 and 3

Now you can AND this with the original value. Since together they have no meaning, you just want to compare whether the ANDed value is non-zero.

bool isKeyPressed = ((original & 0x06) != 0);

This only tells you if EITHER bit 2 OR bit 3 is set, not which of the two are set.

Hope that helps a bit,

James

"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation

GeneralRe: IntPtr to a structure Pin
leppie6-Jan-03 19:43
leppie6-Jan-03 19:43 
GeneralRe: IntPtr to a structure Pin
Heath Stewart7-Jan-03 3:40
protectorHeath Stewart7-Jan-03 3:40 
GeneralRe: IntPtr to a structure Pin
leppie7-Jan-03 6:09
leppie7-Jan-03 6:09 
GeneralRe: IntPtr to a structure Pin
Heath Stewart7-Jan-03 7:56
protectorHeath Stewart7-Jan-03 7:56 
GeneralError in uninstalling the Windows Service Pin
jayakarthikeyan6-Jan-03 3:14
jayakarthikeyan6-Jan-03 3:14 
GeneralRe: Error in uninstalling the Windows Service Pin
Stephane Rodriguez.6-Jan-03 3:35
Stephane Rodriguez.6-Jan-03 3:35 
GeneralRe: Error in uninstalling the Windows Service Pin
jayakarthikeyan6-Jan-03 18:15
jayakarthikeyan6-Jan-03 18:15 
GeneralS.Rod :) Pin
Sijin5-Jan-03 23:55
Sijin5-Jan-03 23:55 
GeneralRe: S.Rod :) Pin
fretre6-Jan-03 1:56
fretre6-Jan-03 1:56 
QuestionHow to insert pictures to RichTextBox? Pin
wei_xiang5-Jan-03 20:56
wei_xiang5-Jan-03 20:56 
AnswerRe: How to insert pictures to RichTextBox? Pin
Stephane Rodriguez.6-Jan-03 3:47
Stephane Rodriguez.6-Jan-03 3:47 
GeneralRe: How to insert pictures to RichTextBox? Pin
wei_xiang6-Jan-03 14:44
wei_xiang6-Jan-03 14:44 
QuestionRunning an external proocess, How? Pin
fesenjoon5-Jan-03 20:48
fesenjoon5-Jan-03 20:48 
AnswerRe: Running an external proocess, How? Pin
Stephane Rodriguez.5-Jan-03 20:51
Stephane Rodriguez.5-Jan-03 20:51 
AnswerRe: Running an external proocess, How? Pin
wei_xiang6-Jan-03 14:35
wei_xiang6-Jan-03 14:35 
GeneralRe: Running an external proocess, How? Pin
Stephane Rodriguez.6-Jan-03 19:08
Stephane Rodriguez.6-Jan-03 19:08 
GeneralNew ECMA submission for VC# 1.1 Pin
SimonS5-Jan-03 20:06
SimonS5-Jan-03 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.