Click here to Skip to main content
15,914,500 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using Methods within a Switch Case Pin
Richard MacCutchan12-Dec-09 3:08
mveRichard MacCutchan12-Dec-09 3:08 
GeneralRe: Using Methods within a Switch Case Pin
DevonDaDude15-Dec-09 18:20
DevonDaDude15-Dec-09 18:20 
AnswerRe: Using Methods within a Switch Case Pin
OriginalGriff11-Dec-09 23:01
mveOriginalGriff11-Dec-09 23:01 
AnswerRe: Using Methods within a Switch Case Pin
PIEBALDconsult12-Dec-09 3:59
mvePIEBALDconsult12-Dec-09 3:59 
GeneralRe: Using Methods within a Switch Case Pin
DevonDaDude12-Dec-09 15:45
DevonDaDude12-Dec-09 15:45 
QuestionUse of << and >> [modified] Pin
Joe Rozario11-Dec-09 19:16
Joe Rozario11-Dec-09 19:16 
AnswerRe: Use of << and >> Pin
Abhinav S11-Dec-09 19:56
Abhinav S11-Dec-09 19:56 
AnswerRe: Use of << and >> [modified] Pin
DaveyM6911-Dec-09 21:27
professionalDaveyM6911-Dec-09 21:27 
<< and >> do a bitwise shift in the indicated direction the specified number of places. Think of them like n x 10 and n / 10 in decimal (base 10), but in binary (base 2) so they are effectively n * 2 and n / 2 if shifting by one.

| does a bitwise OR where each binary digit is ORed with it's counterpart. This is the OR truth table for each bit in two values being ORed, the first column is the first value, the second column is the second value and the final column is the result:
--------
|0|0||0|
|0|1||1|
|1|0||1|
|1|1||1|
--------
A logical OR || is the same except it can only operate on a boolean value and returns a boolean, so replace the 0s in the table with false and the 1s with true.

Unless you're doing binary operations then you won't use them every day. One place I had to use them recently is in getting/creating the constituent bytes of a MIDI packet. All 'short' MIDI messages are between 1 and 3 bytes long. An Int32 is 4 bytes so Microsoft chose to place all packets in one Int32. If I use a NoteOn as an example:
1001cccc : byte0 = status (c = channel)
0nnnnnnn : byte1 = note number
0vvvvvvv : byte2 = velocity

As an Int32: xxxxxxxx0vvvvvvv0nnnnnnn1001cccc

So to build this Int32 from 3 bytes status, note, velocity) I need to start with status, then left shift note by 8 bits and velcity by 16 bits then OR them all.
Int32 intStatus = status;
Int32 intNote = (note << 8);
Int32 intVelocity = (velocity << 16);
Int32 packet = intStatus | intNote | intVelocity;

To get the bytes out, a 'mask' is needed and a logical AND combined with a right shift
Int32 statusMask = 0x000000FF;
Int32 noteMask = 0x00007F00;
Int32 velocityMask = 0x007F0000;

byte status = (byte)(packet & statusMask);
byte note = (byte)((packet & noteMask) >> 8);
byte velocity = (byte)((packet & velocityMask) >> 16;


Dave

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

modified on Saturday, December 12, 2009 1:38 PM

GeneralRe: Use of &lt;&lt; and &gt;&gt; Pin
Joe Rozario11-Dec-09 21:46
Joe Rozario11-Dec-09 21:46 
GeneralRe: Use of &lt;&lt; and &gt;&gt; Pin
DaveyM6911-Dec-09 22:37
professionalDaveyM6911-Dec-09 22:37 
GeneralRe: Use of &lt;&lt; and &gt;&gt; Pin
Joe Rozario16-Dec-09 17:59
Joe Rozario16-Dec-09 17:59 
AnswerRe: Use of &lt;&lt; and &gt;&gt; Pin
harold aptroot11-Dec-09 21:45
harold aptroot11-Dec-09 21:45 
QuestionHow to show multiple crystal reports in single Report Viewer. [modified] Pin
sher_azam11-Dec-09 18:35
sher_azam11-Dec-09 18:35 
GeneralRe: How to show multiple crystal reports in single Report Viewer. Pin
Abhinav S11-Dec-09 23:02
Abhinav S11-Dec-09 23:02 
GeneralRe: How to show multiple crystal reports in single Report Viewer. Pin
sher_azam11-Dec-09 23:13
sher_azam11-Dec-09 23:13 
QuestionCustom User Control for Multipoint SDK! Pin
Kushal Gautam11-Dec-09 17:53
Kushal Gautam11-Dec-09 17:53 
AnswerRe: Custom User Control for Multipoint SDK! Pin
krunal255-Feb-10 1:53
krunal255-Feb-10 1:53 
Questionthreading, invoke Pin
benjamin yap11-Dec-09 16:52
benjamin yap11-Dec-09 16:52 
AnswerRe: threading, invoke Pin
Saksida Bojan12-Dec-09 1:58
Saksida Bojan12-Dec-09 1:58 
QuestionSerialPort UART input buffer omits zeroes Pin
Karthik S Prakash11-Dec-09 15:18
Karthik S Prakash11-Dec-09 15:18 
AnswerRe: SerialPort UART input buffer omits zeroes Pin
Luc Pattyn11-Dec-09 15:54
sitebuilderLuc Pattyn11-Dec-09 15:54 
GeneralRe: SerialPort UART input buffer omits zeroes Pin
Karthik S Prakash12-Dec-09 6:05
Karthik S Prakash12-Dec-09 6:05 
QuestionMultithreading in WinForm/WPF Application Pin
sandeepranjan11-Dec-09 15:00
sandeepranjan11-Dec-09 15:00 
Answercross-post Pin
Luc Pattyn11-Dec-09 15:55
sitebuilderLuc Pattyn11-Dec-09 15:55 
AnswerRe: Multithreading in WinForm/WPF Application Pin
#realJSOP13-Dec-09 3:46
professional#realJSOP13-Dec-09 3:46 

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.