Click here to Skip to main content
15,885,622 members
Home / Discussions / C#
   

C#

 
AnswerRe: Need Help getting bits from bytes. Pin
Ravadre10-Feb-09 7:15
Ravadre10-Feb-09 7:15 
GeneralRe: Need Help getting bits from bytes. [modified] Pin
Muratus10-Feb-09 7:40
Muratus10-Feb-09 7:40 
GeneralRe: Need Help getting bits from bytes. Pin
Ravadre10-Feb-09 9:24
Ravadre10-Feb-09 9:24 
GeneralRe: Need Help getting bits from bytes. Pin
Muratus10-Feb-09 9:56
Muratus10-Feb-09 9:56 
GeneralRe: Need Help getting bits from bytes. Pin
Ravadre10-Feb-09 10:14
Ravadre10-Feb-09 10:14 
GeneralRe: Need Help getting bits from bytes. Pin
Muratus10-Feb-09 10:53
Muratus10-Feb-09 10:53 
AnswerRe: Need Help getting bits from bytes. [modified] Pin
#realJSOP10-Feb-09 9:59
mve#realJSOP10-Feb-09 9:59 
AnswerRe: Need Help getting bits from bytes. Pin
Alan N10-Feb-09 14:59
Alan N10-Feb-09 14:59 
This is how you extract a range of bits from an integer value using a mask and bit shifting. If you understand this you can do anything!
Given the example number 179
10110011  number in binary
--+++---
00111000  mask to extract bits 3-5

00110000  intermediate result
00000110  final result

76543210  bit indices

The mask will extract the 3 bits marked with the +'s.

These are the 4 logical steps in the process

1) create a bit mask to cover the region of interest, 
   in this case 3 bits,  binary 111 or hex 0x07
   Byte mask = 0x07;
   
2) shift the mask into the correct position 
   In the example the mask starts at bit 3
   Byte mask = 0x07 << 3;  // mask is now binary 111000
   
3) Apply the mask to the number with the bitwise AND operation.
   This allows the bits from the number to fall through 
   the mask into the intermediate result.
   
   Int32 intermediateResult = number & mask;
 or
   Int32 intermediateResult = number & (0x07 << 3);
   // the value is 110000
   
   Note that when we do this the result is automatically widened to 32 bits.
   
4) Shift the intermediate result to the right to get the actual value
   of the bits of interest.
   
   Int32 result = intermediateResult >> 3;
 or
   Int32 result = (number & (0x07 << 3)) >> 3;
   // result is binary 110 or 0x06 or decimal 6
   
   As result was widened to 32 bits we can cast back to a byte if desired.
   Take note of all the parentheses.
   
   Byte result = (Byte)((number & (0x07 << 3)) >> 3);
   
   If you want to see the result as a binary formatted string
   
   String s = Convert.ToString(result, 2).PadLeft(8, '0');
   // s is "00000110"


Here endeth the first lesson. Hope that helps a 'bit' and apologies for the pun.

Alan.
QuestionUpload file to sharepoint without/without sharepoint.dll Pin
Burdzy10-Feb-09 6:17
Burdzy10-Feb-09 6:17 
AnswerRe: Upload file to sharepoint without/without sharepoint.dll Pin
musefan10-Feb-09 6:28
musefan10-Feb-09 6:28 
GeneralRe: Upload file to sharepoint without/without sharepoint.dll Pin
Burdzy10-Feb-09 6:48
Burdzy10-Feb-09 6:48 
QuestionNetwork info Pin
Froz3n10-Feb-09 5:58
Froz3n10-Feb-09 5:58 
QuestionStreamReader.ReadLine() Pin
staticv10-Feb-09 5:19
staticv10-Feb-09 5:19 
AnswerRe: StreamReader.ReadLine() Pin
Dave Kreskowiak10-Feb-09 5:25
mveDave Kreskowiak10-Feb-09 5:25 
AnswerRe: StreamReader.ReadLine() Pin
Rob Philpott10-Feb-09 5:28
Rob Philpott10-Feb-09 5:28 
GeneralRe: StreamReader.ReadLine() [modified] Pin
staticv10-Feb-09 5:50
staticv10-Feb-09 5:50 
AnswerRe: StreamReader.ReadLine() Pin
Luc Pattyn10-Feb-09 7:08
sitebuilderLuc Pattyn10-Feb-09 7:08 
Question'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
abbd10-Feb-09 5:16
abbd10-Feb-09 5:16 
AnswerRe: 'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
Dave Kreskowiak10-Feb-09 5:23
mveDave Kreskowiak10-Feb-09 5:23 
GeneralRe: 'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
abbd10-Feb-09 6:05
abbd10-Feb-09 6:05 
GeneralRe: 'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
Dave Kreskowiak10-Feb-09 6:38
mveDave Kreskowiak10-Feb-09 6:38 
AnswerRe: 'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
Luc Pattyn10-Feb-09 7:12
sitebuilderLuc Pattyn10-Feb-09 7:12 
GeneralRe: 'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
abbd10-Feb-09 21:53
abbd10-Feb-09 21:53 
AnswerRe: 'WindowsApplication1.encode.Image' to 'System.Drawing.Image' Pin
Luc Pattyn11-Feb-09 0:06
sitebuilderLuc Pattyn11-Feb-09 0:06 
QuestionSQLServer Query Problem - Solved ... thx Pin
benny merkle10-Feb-09 4:50
benny merkle10-Feb-09 4:50 

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.