Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Setting of a binary value (http://www.go4expert.com/forums/setting-binary-value-t2277/)

 felisca 18Dec2006 01:45

Setting of a binary value

Is there is some option to set binary value to a variable in c++ ?

 felisca 18Dec2006 03:33

Re: Setting of a binary value

No need, thanks! I found the answer.

 Aztec 18Dec2006 05:47

Re: Setting of a binary value

Quote:
 Originally Posted by felisca No need, thanks! I found the answer.
and that is?

 shabbir 18Dec2006 05:49

Re: Setting of a binary value

Quote:
 Originally Posted by felisca No need, thanks! I found the answer.
Would you mind sharing that?

 DaWei 18Dec2006 06:28

Re: Setting of a binary value

I'll make some general comments. All values stored in your computer (run of the mill computer of today, anyway) are binary. They are stored as ones and zeroes. The interpretation of that value may vary. This means that you always need to consider what the pattern represents, as well as the actual pattern of ones and zeroes. A byte with the value, 0011 0001, may represent a character, '1'. The value, 0000 0001, may represent the integer, 1. A floating point 1 would be a different pattern entirely. A 2-of-5 code for 1 would be yet another pattern. The pattern sent by a keyboard when you press 1 may be none of the above.

It may seem confusing at first, but it's something you must get your head around. Octal, hexadecimal, and decimal representations are a different way of expressing a binary pattern. Again, the meaning of the pattern may change, strictly because you want it to represent something specific that has no one-to-one correspondence with the actual pattern that exists in the storage element.

Further, just to make things worse, if more than one byte is used to store a pattern, the order of the bytes may vary from platform to platform (little endian versus big endian versus various mixed forms of the two). Fortunately, your language and various standards try to insulate you from the endian issue as much as possible.

 felisca 18Dec2006 18:16

Re: Setting of a binary value

Quote:
 Would you mind sharing that?
First of all the answer is no. The only way that I found is bitwise operations.
Here is how I implemented this:

char soh = '/a'; // I din't find short form of soh,if you know, tell me please
soh=soh>>2;

string myString = "10011100";
char myChar = '/0';
for (i=0;i<8;i++)
{
myChar= myChar << 1;
if(myString[i]=='1'){myChar= (myChar|soh);}
}

 felisca 18Dec2006 18:21

Re: Setting of a binary value

Quote:
 Originally Posted by DaWei I'll make some general comments. All values stored in your computer (run of the mill computer of today, anyway) are binary. They are stored as ones and zeroes. The interpretation of that value may vary. This means that you always need to consider what the pattern represents, as well as the actual pattern of ones and zeroes. A byte with the value, 0011 0001, may represent a character, '1'. The value, 0000 0001, may represent the integer, 1. A floating point 1 would be a different pattern entirely. A 2-of-5 code for 1 would be yet another pattern. The pattern sent by a keyboard when you press 1 may be none of the above. It may seem confusing at first, but it's something you must get your head around. Octal, hexadecimal, and decimal representations are a different way of expressing a binary pattern. Again, the meaning of the pattern may change, strictly because you want it to represent something specific that has no one-to-one correspondence with the actual pattern that exists in the storage element. Further, just to make things worse, if more than one byte is used to store a pattern, the order of the bytes may vary from platform to platform (little endian versus big endian versus various mixed forms of the two). Fortunately, your language and various standards try to insulate you from the endian issue as much as possible.

All is true,but doesn't mean that we don't need to operate with bits directly.

 All times are GMT +5.5. The time now is 18:09.