Funny bit games

I

Isliguezze

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++?
 
I

Isliguezze

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?
 
R

R.A. Nagy

Isliguezze said:
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
 
R

R.A. Nagy

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 said:
R.A. Nagy said:
[..]
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;
}

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
 
I

Isliguezze

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++?
 
M

Mirco Wahab

Isliguezze said:
Nice hack actually, bu I don't think that I do really understand this
string:


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.
 
M

Michael Oswald

Isliguezze said:
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) >> 8);
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) << 8) |
lowest;


hth,
Michael
 
J

James Kanze

Nice hack actually, bu I don't think that I do really understand this
string:
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 >> 8) & 0xFF ;
dest[ 2 ] = (value ) & 0xFF ;
}

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

R.A. Nagy

Isliguezze said:
Nice hack actually, bu I don't think that I do really understand this
string:


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++?

The code just assigns a bit pattern to whatever the architecture defines as
an int. The example had nothing to do with your 8 + 8 + 1 problem. Sorry.

Also understand that, be they signed or unsigned, that characters typically
have the same number of bits. (I was surprised to ready of your problem.
Does not happen too offend!)

FWIW, consider running the snippet on Intel 32 - Notice how it actually
prints the characters backward - loosely reflects the underlying
architecture of the processor (little-endian.)

I was just a fun fact - er ... hack - Motorola would do something else. One
of the reasons why bit shifting / masking is a superior solution for your
particular problem. Also the reason for the irritated comments.

..02 ends.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top