Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Funny bit games

Reply
Thread Tools

Funny bit games

 
 
Isliguezze
Guest
Posts: n/a
 
      06-24-2008
Hi. There's a task of splitting an int into three unsigned char's.
There's also one limitation. The maximum size of the given int is
10^9, what's enough to code with as small as 17 bits. So, what is the
problem? How to split a maximum 17 value bits containing int into 3
unsigned chars(8+8+1, 6 bits)? The problem is that I can't use
unsigned char pointer, to point on int like this:

unsigned char *uchp;
unsigned int i = 1000000000;

uchp = &i;

for (int j = 0; j < sizeof(int); ++j)
cout << *uchp;

to point on each byte of int... What can be performed in assembler
using BYTE PTR, architecture is Intel x86. But how to split int into
three unsigned chars using C++?
 
Reply With Quote
 
 
 
 
Isliguezze
Guest
Posts: n/a
 
      06-24-2008
> See the bitwise operator AND (&) and the right shift operator (>>).
> Apply the mask, then shift.


What do I need the mask for? What is mask? How is it constructed?
 
Reply With Quote
 
 
 
 
R.A. Nagy
Guest
Posts: n/a
 
      06-24-2008
"Isliguezze" <> wrote in message
news:83ca9858-086f-4942-8f81-...
> Hi. There's a task of splitting an int into three unsigned char's.
> There's also one limitation. The maximum size of the given int is
> 10^9, what's enough to code with as small as 17 bits. So, what is the
> problem? How to split a maximum 17 value bits containing int into 3
> unsigned chars(8+8+1, 6 bits)? The problem is that I can't use
> unsigned char pointer, to point on int like this:
>
> unsigned char *uchp;
> unsigned int i = 1000000000;
>
> uchp = &i;
>
> for (int j = 0; j < sizeof(int); ++j)
> cout << *uchp;
>
> to point on each byte of int... What can be performed in assembler
> using BYTE PTR, architecture is Intel x86. But how to split int into
> three unsigned chars using C++?


While this is not the same as what you are discussing, packing / unpacking 8
bits using a union can come in handy from time to time:

-----

#include <iostream>

using namespace std;

void main(int argc, char *argv[])
{
typedef int XINT;
typedef char XBYTES[4];

union sigma
{
XINT xint;
XBYTES xbytes;
} test;

test.xint = 'ABCD';
cout << test.xbytes[0] << endl;
cout << test.xbytes[1] << endl;
cout << test.xbytes[2] << endl;
cout << test.xbytes[3] << endl;
}

-----

IMO far too few is the opportunity to discuss the virture of unions. While
we are not framing more than 8 bits (as requested), the technique is one for
a few new-of-us to be aware of. (i.e. 'funny bit games'

R.A. Nagy
http://www.Soft9000.com


 
Reply With Quote
 
R.A. Nagy
Guest
Posts: n/a
 
      06-24-2008

"Isliguezze" <> wrote in message
news:52ae4ae5-1f4d-4407-9e49-...
>> See the bitwise operator AND (&) and the right shift operator (>>).
>> Apply the mask, then shift.

>
> What do I need the mask for? What is mask? How is it constructed?


Here is an adequate place to start:

http://en.wikipedia.org/wiki/Mask_(computing)



 
Reply With Quote
 
R.A. Nagy
Guest
Posts: n/a
 
      06-24-2008

"R.A. Nagy" <> wrote in message
news:WG98k.20$Eg.5@trnddc01...
>
> "Isliguezze" <> wrote in message
> news:52ae4ae5-1f4d-4407-9e49-...
>>> See the bitwise operator AND (&) and the right shift operator (>>).
>>> Apply the mask, then shift.

>>
>> What do I need the mask for? What is mask? How is it constructed?

>
> Here is an adequate place to start:
>
> http://en.wikipedia.org/wiki/Mask_(computing)
>
>
>


Conceptual:

http://en.wikipedia.org/wiki/Bitwise_operations


 
Reply With Quote
 
R.A. Nagy
Guest
Posts: n/a
 
      06-24-2008
Oh, you are so right - But I though we were talking about Intel here? A
defined architecture?

I also believe that I mentioned that this was not in keeping with what the
original questions was ...??

But either way Victor, thank you for the correction. I am humbled by your
tact, humility, and overall demeanor.

YOU ARE THE MAN!!






"Victor Bazarov" <> wrote in message
news:g3r99e$93q$...
> R.A. Nagy wrote:
>> [..]
>> While this is not the same as what you are discussing, packing /
>> unpacking 8 bits using a union can come in handy from time to time:
>>
>> -----
>>
>> #include <iostream>
>>
>> using namespace std;
>>
>> void main(int argc, char *argv[])

>
> Try to make it 'int main'. Teaching 'void main' is just wrong. Now, do
> you use 'argc' and 'argv'? If not, why declare them? So, consider
>
> int main()
>
>> {
>> typedef int XINT;
>> typedef char XBYTES[4];
>>
>> union sigma
>> {
>> XINT xint;
>> XBYTES xbytes;
>> } test;
>>
>> test.xint = 'ABCD';
>> cout << test.xbytes[0] << endl;
>> cout << test.xbytes[1] << endl;
>> cout << test.xbytes[2] << endl;
>> cout << test.xbytes[3] << endl;
>> }
>>
>> -----
>>
>> IMO far too few is the opportunity to discuss the virture of unions.

>
> Virtue? What you do here has undefined behaviour. Use of the union is
> only defined if you access the same value you stored.
>
> > While
>> we are not framing more than 8 bits (as requested), the technique is one
>> for a few new-of-us to be aware of. (i.e. 'funny bit games'

>
> This is not a technique. It's a hack. Besides, it relies on the 'int' to
> have the size of 4 char which isn't necessarily so. Also, the OP asked
> for "unsigned char"...
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask



 
Reply With Quote
 
Isliguezze
Guest
Posts: n/a
 
      06-25-2008
Nice hack actually, bu I don't think that I do really understand this
string:

> test.xint = 'ABCD';


What does it do? I need to convert 17 bit int to three unsigned chars
in such way to have it all look like this: 8 bits + 8 bits + 1 bit,
and how to restore them back to an int? I didn't hear proper answer,
how do I do that in C++?
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      06-25-2008
Isliguezze wrote:
> Nice hack actually, bu I don't think that I do really understand this
> string:
>
>> test.xint = 'ABCD';

>
> What does it do? I need to convert 17 bit int to three unsigned chars
> in such way to have it all look like this: 8 bits + 8 bits + 1 bit,
> and how to restore them back to an int? I didn't hear proper answer,
> how do I do that in C++?


I didn't understand what this is good for and
what constraints are given (range, speed, purpose).

If it's only to prove a pint, you could do it w/
bitset and regex, with no shifting/masking at all:



#include <bitset>
#include <string>
#include <iostream>
#include <boost/regex.hpp>

int main()
{
unsigned long uch[3];
unsigned int i = 2;
using namespace std;

string s = bitset<17>(i).to_string();
boost::smatch m;

if(boost::regex_match(s, m, boost::regex("^(.{8})(.{8})(.{1})$"))) {
uch[0] = ( bitset<8>(m[1].str()) ).to_ulong();
uch[1] = ( bitset<8>(m[2].str()) ).to_ulong();
uch[2] = ( bitset<1>(m[3].str()) ).to_ulong();
}
cout << "number: " << i << ", bitmap: " << s << endl
<< "8 bit, value: " << uch[0] << endl
<< "8 bit, value: " << uch[1] << endl
<< "1 bit, value: " << uch[2] << endl;

return 0;
}


Regards

M.
 
Reply With Quote
 
Michael Oswald
Guest
Posts: n/a
 
      06-25-2008
Isliguezze wrote:
> I need to convert 17 bit int to three unsigned chars
> in such way to have it all look like this: 8 bits + 8 bits + 1 bit,
> and how to restore them back to an int? I didn't hear proper answer,
> how do I do that in C++?


You already got a link to wikipedia, where you can find what you need.

But just to get the idea, maybe you want something like (untested, and
perhaps the casts are not necessary):

uint8_t lowest, higher, evenHigher
unsigned int num = <the integer to convert>

low = static_cast<uint8_t>(num & 0xff);
higher = static_cast<uint8_t>((num & 0xff00) >> ;
evenHigher = static_cast<uint8_t>((num & 10000) >> 16);


And the reverse:

unsigned int num2 = (static_cast<unsigned int>(evenHigher) << 16) |
(static_cast<unsigned int>(higher) << |
lowest;


hth,
Michael
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-25-2008
On Jun 25, 4:28 pm, Isliguezze <isligue...@gmail.com> wrote:
> Nice hack actually, bu I don't think that I do really understand this
> string:


> > test.xint = 'ABCD';


> What does it do?


Whatever the implementation wants it to do. You'll have to look
up the documentation of your implementation to find out.

In general, multibyte character literals are a misfeature, still
supported for reasons of backward compatibility, but not
something anyone should use, or even worry about understanding.

> I need to convert 17 bit int to three unsigned chars
> in such way to have it all look like this: 8 bits + 8 bits + 1 bit,
> and how to restore them back to an int? I didn't hear proper answer,
> how do I do that in C++?


Someone did mention shifting and the bitwise operators, it
seems. But you've not really specified enough: which bits
belong in what character. If I suppose network byte order, and
put the bit 16 (the high order bit) in the first byte, the
correct answer would be something like:

void
to3Bytes( unsigned char* dest, int value )
{
assert( value >= 0 && value < (1 << 17 ) ) ;
dest[ 0 ] = (value >> 16) & 0xFF ;
dest[ 1 ] = (value >> & 0xFF ;
dest[ 2 ] = (value ) & 0xFF ;
}

int
from3Bytes( unsigned char* source )
{
return dest[ 0 ] << 16
| dest[ 1 ] << 8
| dest[ 2 ] ;
}

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
games ....games...games...games.. gftr23@gmail.com Computer Support 0 03-08-2008 09:39 AM
games ....games...games...games.. gftr23@gmail.com Digital Photography 0 03-08-2008 09:38 AM
online games, free online games, games to play online princess Computer Support 0 05-13-2007 10:58 AM
online games, free online games, games to play online princess Computer Support 0 05-10-2007 09:25 AM
online games, free online games, games to play online princess Computer Support 0 05-10-2007 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57