|
I need a program that operate with bit.
Example:
unsigned long x;
I want to put 1 or 0 to the 6th bit in x.
Can you help me?
lang tu
|
|
|
|
|
Well, the numbers you want for each bit are:
1
2
4
8
16
32
64
128
256
512
etc.
So, x |= 128 will set the 8th bit. x &= ~128 will strip that bit.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
If you are saying the the eighth bit is the 27 bit you want to be set:
<br />
unsigned int v = 1;<br />
<br />
v |= 1 << 7;
<br />
v |= 1 << 6;
<br />
v ^= 1 << 6;
<br />
v ^= 1 << 0;
<br />
v ^= 1 << 0;
-- modified at 22:46 Wednesday 29th November, 2006
|
|
|
|
|
Set (1) high order bit of a byte:
unsigned char ByTe = 0;
byte |= 0x80;
Reset (0) high order bit of a byte:
unsigned char ByTe = 0x80;
byte &= ~0x80;
|
|
|
|
|
Do vectors or CObArrays (and other similar containers/classes) automatically free their memory when the object containing them is destroyed? In other words, I have a vector as a data member in my class. When my class is destroyed, does the vector free whatever memory it held, or do I have to explicitly do that in my classes destructor?
Thank you,
BP
|
|
|
|
|
I believe the containers allocate space for the collection of types and destroys the memory for all those types. However, if the types are objects that contain dynamically allocated memory, the objects are responsible for deallocating their memory. Also, if the types are pointers to dynamically allocated objects, then you are responsible to write code to deallocate the memory.
|
|
|
|
|
It depends. A vector will dispose of an object, but it will not call delete, if the object was a pointer. So, if the object is a class, and it has a destructor, it will be called. If it's a pointer, the memory will leak if you don't delete it.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Thanks for clarifying what I said! I think I need to stop and go to bed.
|
|
|
|
|
Thanks, I appreciate your responses.
|
|
|
|
|
I would like to know that how to add onTimer in .NET as in VS6.0 with ClassWizard?
Please help!
|
|
|
|
|
Thank you everybody!
I solve the problem now!
|
|
|
|
|
LaHaHa wrote: Thank you everybody!
You're welcome!
|
|
|
|
|
#include <string.h><br />
#include <stdio.h><br />
<br />
char string[] = "A string\tof ,,tokens\nand some more tokens";<br />
char seps[] = " ,\t\n";<br />
char *token;<br />
<br />
void main( void )<br />
{<br />
printf( "%s\n\nTokens:\n", string );<br />
<br />
token = strtok( string, seps );<br />
while( token != NULL )<br />
{<br />
<br />
printf( " %s\n", token );<br />
<br />
token = strtok( NULL, seps );<br />
}<br />
}<br />
that is straight from msdn it does what i need but
char string[] = "A string\tof ,,tokens\nand some more tokens";
i need that line to take the text from a edit box in the form of a CString and i dont know how to do it.
|
|
|
|
|
The EditBox has a GetWindowText method, which returns a CString. CString has a GetBuffer method that you can use to get a char *, which is the same as a char[].
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
error C2440: 'initializing' : cannot convert from 'char *' to 'char []
this is new to me.
|
|
|
|
|
I'd expect a cast to fix that.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
you lost me there i can do simple things i dont even know what a cast is.
|
|
|
|
|
i think i got it
char *text = m_text.GetBuffer(m_len);
strcpy( text, m_text ); // directly access CString buffer
m_text.ReleaseBuffer( ); <--- added them 3 lines
//char string[] = text; <--- took that line out
|
|
|
|
|
Yes, this copies it across. A cast would be passing text like this:
(char[]) text
A cast changes the type of an object, assuming the cast is valid.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
i need the conversion from binary to decimal and vice versa but in a very simple way without using libraries
jad
|
|
|
|
|
Binary and decimal are rendering details. The actual values are the same. PErhaps you need to explain what you want to do ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
the program asks the user to input a positive integer and converts it to binary and output the result.
Or the program asks the user to input a binary number and conerts it to decimal and output the result.
this is an example i donno if its right:
#include <iostream.h>
int main()
{
int i;
long int binary;
int decimal = 0;
cout << "Please enter a 8-bit binary number" << endl;
cin>> binary;
for(i = 0; i < 8; i++)
{
if(binary % 10)
{
decimal += 1 << i;
}
binary /= 10;
}
cout << "Your number in decimal is: " << decimal <
|
|
|
|
|
Oh, I see. If you run windows calculator, you can set it in scientific mode, and then it will swap between decimal and binary so you can check your results. Yes, at first glance, this looks right.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
and whats the code from decimal to binary?
jadov
|
|
|
|
|
Well, try reversing the process you have now.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|