|
Hi, I'm new to C# and I'm having some trouble with bit manipulation.
I have Two bytes. The first thing i want to do is to shift the data in one of the bytes four places normally i would do it like this.
byte1 = 00001111
byte1 = byte1 << 4
byte1 = 11110000
however, this seems to work only for integers?
once i have shifted the data i would like to add them together like this.
byte 1 = 11110000
byte 2 = 00001111
bytes added together = 11111111
What is the best way of doing this?
Thank you.
modified on Tuesday, June 1, 2010 10:56 AM
|
|
|
|
|
Either post this as a QA or in a programming forum - but not both. Otherwise it may get answered by different people, who will get annoyed when they find they are duplicating effort.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
I think what you are encountering is the error that there is no implicit cast from an int to a byte. That's because after any such operation like addition or shift etc "small types" are turned into an int. That's not so bad, though, as you can just cast it back to a byte.
For example:
byte b1 = 15;
byte b2 = (byte)(b1 << 4);
byte b3 = (byte)(b1 | b2);
|
|
|
|
|
It works, but .net insists on up-casting byte and short operations to int ; you need to cast back to byte:
byte a = 15 ;
a = (byte) ( a << 4 ) ;
|
|
|
|
|
Thank you guys. Sorry for the double post. I first tried the quick answers section but after i posted my questions i could not locate it in the list. that is why i tried here again.
Thanks again for the fast reply!
|
|
|
|
|
myreki wrote: I first tried the quick answers section but after i posted my questions i could not locate it in the list
Near the top right of the page is a link that may say 'Show Viewed' you might want to click that. I think the default is that once you have viewed a question in QA you will not see it in the list again. Seems a little stupid to me but, there it is.
My current favourite quote is: Punch them in the face, see what happens!
-SK Genius
|
|
|
|
|
Ahh, I see.. I completely missed that. Thanks for the heads up
|
|
|
|
|
Hello,
I need to create a child process in C#. The main process should wait until the child process is done. The execution goes in this order:
/////////////////////////////////
parent process executes some code
parent process creates a child process and goes to sleep
child process executes some code
child process is done executing the code
parent process wakes up
parent process continues execution
///////////////////////////////////
I am looking for some simple implementation i.e. something like the fork() system call available in the C programming language. I don't want to create a new Thread. Any ideas how to achieve this result?
Thank you!
Best,
Martin
|
|
|
|
|
|
If the parent is only going to sleep while the child works, why bother having the child?
martingeorgiev wrote: I don't want to create a new Thread
You can't create an old one.
|
|
|
|
|
It sounds like a great plan: have fun, let others do the work.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
That's what delegates are for.
|
|
|
|
|
I tried using delegates. It is giving me the same error message. see my other post below.\
Thanks!
|
|
|
|
|
|
My code goes like this:
1. gets the indices of the selected items on the form ( ListView ).
2. the form gets updated.
3. restores the selection on the selected items.
Now, if I process the code as stated above, when trying to restore the selection, I get an error message saying that the index is not available. However, if I put a message box right before the selection code ( maybe to slow down ... I don't really know what the difference is ) kicks in, everything works fine.
What might be causing this problem? I have been trying to figure this out for quite a long time.
Thanks a lot!
Best,
Martin
|
|
|
|
|
I've never used a ListView. Are you saying it loses state when the Form is updated? That doesn't make any sense to me. You should find out what causes it before trying to find a work-around.
|
|
|
|
|
The main application fetches data from the database. How to display a progress bar while this fetching is in progress?
|
|
|
|
|
Well, you know that you want to use a BackgroundWorker (your thread title gives this away). This has an event to report the ProgressChanged . Basically, you need to call ReportProgress from your worker thread, and this raises the ProgressChanged event where you can update the progress bar. When you instantiate your background worker, you have to set WorkerReportsProgress to true to be able to report the progress.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
problem is :it displays the progress bar after it has finished fetching !!
|
|
|
|
|
when user clicks an item in the toolbar , the main thread fetches the corresponding data. Meanwhile the background worker thread is supposed to show progress . But instead it is activated only after the fetching is done. any suggestions on how to improve this?
A snippet of my code is as given below:
< my code>
private void toolStripMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
bwProgress.RunWorkerAsync();
}
private void bwProgress_DoWork(object sender, DoWorkEventArgs e)
{
// update the progress bar
for (int i = 0; i <= 100; i+=10)
{
bwProgress.ReportProgress(i);
}
}
private void bwProgress_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripProgressBar1.Visible = true;
toolStripProgressBar1.Value = e.ProgressPercentage;
}
private void bwProgress_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
toolStripProgressBar1.Value = 100;
}
|
|
|
|
|
The problem with your code sample here is that it runs too fast for you to see the progress changed event in action. You need a lengthy operation to see the effect, so youy should really pause the execution between each iteration through the loop.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
That would suggest you're using it in the wrong way. Here's a small sample demonstrating how to update a progress bar while a loop is incrementing,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace SampleBackgroundWorker
{
public partial class MainForm : Form
{
private BackgroundWorker _worker = new BackgroundWorker();
private static readonly object SyncLock = new object();
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_worker.WorkerReportsProgress = true;
_worker.ProgressChanged += new
ProgressChangedEventHandler(ProgressChanged);
_worker.DoWork += new DoWorkEventHandler(DoWork);
_worker.RunWorkerAsync();
}
void DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 10; i++)
{
lock (SyncLock)
{
Monitor.Wait(SyncLock, 1000);
}
_worker.ReportProgress(i * 10);
}
}
void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
barProgress.Value = e.ProgressPercentage ;
}
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Little demo in this tip[^]
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
|
Hi,
I am getting the error while saving the excel work sheet in the windows 2003 server. Same piece of code is working fine in my local desktop. below is the error message. Given required permissions to the folder to IUSR, IWAM, Network service and ASP.NET accounts. Though i am getting the below error
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a.
Getting error in the below piece of code-
Line 1005: Microsoft.Office.Interop.Excel.ApplicationClass objApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Any help is appriciate.
Thanks In Advance.
|
|
|
|