473,413 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Funny bit games

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++?
Jun 27 '08 #1
13 1380
Isliguezze wrote:
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)?
What do you mean by "(8+8+1, 6 bits)"? How do the 17 bits map into the
bits of the three bytes?
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++?
See the bitwise operator AND (&) and the right shift operator (>>).
Apply the mask, then shift.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
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?
Jun 27 '08 #3
"Isliguezze" <is********@gmail.comwrote in message
news:83**********************************@k13g2000 hse.googlegroups.com...
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
Jun 27 '08 #4

"Isliguezze" <is********@gmail.comwrote in message
news:52**********************************@l64g2000 hse.googlegroups.com...
>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)

Jun 27 '08 #5

"R.A. Nagy" <r.******@gmail.comwrote in message
news:WG98k.20$Eg.5@trnddc01...
>
"Isliguezze" <is********@gmail.comwrote in message
news:52**********************************@l64g2000 hse.googlegroups.com...
>>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
Jun 27 '08 #6
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
Jun 27 '08 #7
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" <v.********@comAcast.netwrote in message
news:g3**********@news.datemas.de...
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

Jun 27 '08 #8
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++?
Jun 27 '08 #9
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.
Jun 27 '08 #10
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) >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
Jun 27 '08 #11
James Kanze wrote:
[..]

int
from3Bytes( unsigned char* source )
int
from3Bytes(unsigned char const* source) // 'const' just in case...
{
return dest[ 0 ] << 16
| dest[ 1 ] << 8
| dest[ 2 ] ;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #12

"Isliguezze" <is********@gmail.comwrote in message
news:2b**********************************@a1g2000h sb.googlegroups.com...
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++?
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.
Jun 27 '08 #13
On Jun 25, 7:31 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
[..]
int
from3Bytes( unsigned char* source )
int
from3Bytes(unsigned char const* source) // 'const' just in case...
Yes. And not just in case; you want to be able to call the
function (without a const cast) from another function which
received the buffer as const.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jun 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
761
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
4
by: ώε↮øй | last post by:
I have the whole group of .NET 2003 platforms sitting here from a dream I have of building some simple web based games (much like on msn zone) Any guidance as to where I start and what I need to...
4
by: viper888 | last post by:
Hi to all, I'm the newly appointed network administrator in our office, and upon scanning all the PCs that are connected to the network, (by the way we're using a windows 2000 server) almost all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.