473,386 Members | 1,741 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,386 software developers and data experts.

9 bits crammed in to 8 bits space...

imagine a hollow cube.
every wall can have one of two states: open or closed. This would take
6 bits to represent.
Now, i allso neew to know whitch way i entered the room, this could be
representet with a integer from 0 to 5 = 3 bits.
As 2^6*6-1 = 383 and an 8 bit integer can hold a max value of 255, we
ofcourse have to do some creative thinking, it is however, not as hard
as it sounds, because the wall/door/direction in witch i entered the
room doesnt need a state, so now we are at 2^5*6-1 = 191 and thats a
great deal lower than 255.
However, with this aproach one problem remains.
The walls/doors no longer have a fixes position in the bit field, fx.
if the direction from witch the room was entered is 5 and therefor
wall/door no. 6 will be represented by bit no. 8 (as the 6th wall/door
doesnt need to be represented), if the direction ID is 4, the 5th
wall/door, will not be represented, it doesnt have to be. If the the
direction ID is 3 the 5th wall/door will be representet by the 7th bit,
ect.

This is all a bit confusing and im not quite sure i have explained it
good enough, however i have written a "class" that in my oppinion
should work, but dont(it compiles fine though, but the result is
wrong). maybe that could give some inspiration.

@code start
class Stack {
private:
std::vector<unsigned char> P_Stack;
unsigned char T_Stack;
public:
Stack() {
T_Stack = 0;
}
~Stack() {}
bool Put_Dir(int a) { // should work
T_Stack = (a >= 0 && a < 6) ? a : T_Stack;
}
void Put_Door(int a) { // should work
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
T_Stack |= (1 << a);
}
}
unsigned int Get_Dir() { //doesnt work
return (T_Stack & 7);
}
bool Get_Door(int a) { // seems to work, but missing door = 1
implementation is missing.
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
return (T_Stack & (1 << a));
}
}
bool Dead_End() { // probably doesnt work
if (T_Stack & (31 << 3)) {
return 1;
}
else {
return 0;
}
}
void Next() { // should work
P_Stack.push_back(T_Stack);
T_Stack = 0;
}
void Pop() { // should work
P_Stack.pop_back();
T_Stack = P_Stack.back();
}
};
class Grid {};
int main() {
Stack stack;
stack.Put_Dir(0);
stack.Put_Door(1);
stack.Put_Door(2);
stack.Put_Door(3);
stack.Put_Door(4);
stack.Put_Door(5);
std::cin.get();
// when i ask for direction the result should be 0.
// when i ask for door 0 the result should be 1, but this information
isnt stored in the bitfield.
// when i ask for door 1-5 the result in thiscase should allso be 1,
but only because i have set them, otherwise the should be 0;
}
@code end

Regards
Felix Zacariaz Bloutarski

Feb 24 '06 #1
4 1508

fe**********@hotmail.com schrieb:
void Put_Door(int a) { // should work
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
T_Stack |= (1 << a);
}
}
unsigned int Get_Dir() { //doesnt work
return (T_Stack & 7);
} bool Get_Door(int a) { // seems to work, but missing door = 1
implementation is missing.
if (a >= 0 && a < 6) {
if (a >= (T_Stack & 7)) {
a--;
}
return (T_Stack & (1 << a));
}
}

the "<<" ignores the 3 bits used for the "comming from direction"

Feb 24 '06 #2
posted:
imagine a hollow cube.
every wall can have one of two states: open or closed. This would take
6 bits to represent.
Six sides, each of two possible states, so 6 bits.

I agree.
Now, i allso neew to know whitch way i entered the room, this could be
representet with a integer from 0 to 5 = 3 bits.
There are six possible combinations for which side you used to enter the
room.

A 3-Bit number has eight possible combinations.

You're wasting the last two of the combinations.

As 2^6*6-1 = 383 and an 8 bit integer can hold a max value of 255, we
ofcourse have to do some creative thinking, it is however, not as hard
as it sounds, because the wall/door/direction in witch i entered the
room doesnt need a state, so now we are at 2^5*6-1 = 191 and thats a
great deal lower than 255.

Good thinking.

So let's say you entered the room through the side 2:

Bit 0: side 0 = closed
Bit 1: side 1 = open
Bit 2: side 3 = closed //We've omitted side 2
Bit 3: side 4 = open
Bit 4: side 5 = closed

Now we've three further bits to specify by which side we entered the room.
(ie. 0 to 5)
Makes sense.
room doesnt need a state
However, with this aproach one problem remains.
The walls/doors no longer have a fixes position in the bit field, fx.
if the direction from witch the room was entered is 5 and therefor
wall/door no. 6 will be represented by bit no. 8 (as the 6th wall/door
doesnt need to be represented), if the direction ID is 4, the 5th
wall/door, will not be represented, it doesnt have to be. If the the
direction ID is 3 the 5th wall/door will be representet by the 7th bit,
ect.
Correct. The order has been disturbed.
Maybe that could give some inspiration.

Here's some code I just wrote. It's just to get you going; it may contain
errors and bugs as I wrote it quickly and sloppily.

class PresenceInRoom
{
private:

char data; //Stores our data

struct SixBools
{
bool side_status[6];
};
bool GetBit(unsigned i)
{
return i && ( 1U << (i - 1) );
}

SixBools GetSixBools()
{
//First we determine which side we came in through

unsigned side_came_in = 0;

if GetBit( 5 ) side_came_in += 4;
if GetBit( 6 ) side_came_in += 2;
if GetBit( 7 ) side_came_in += 1;

//Now we have our side

SixBools six_bools;

six_bools[side_came_in] = false;

{
unsigned bit = 0;

for (unsigned side = 0; side < 6; ++side, ++bit)
{
if (side == side_came_in)
{
--bit;
continue;
}

six_bools[side] = GetBit( bit );
}
}

}
};

-Tomás
Feb 24 '06 #3

Tomás skrev:
posted:
imagine a hollow cube.
every wall can have one of two states: open or closed. This would take
6 bits to represent.


Six sides, each of two possible states, so 6 bits.

I agree.
Now, i allso neew to know whitch way i entered the room, this could be
representet with a integer from 0 to 5 = 3 bits.


There are six possible combinations for which side you used to enter the
room.

A 3-Bit number has eight possible combinations.

You're wasting the last two of the combinations.

As 2^6*6-1 = 383 and an 8 bit integer can hold a max value of 255, we
ofcourse have to do some creative thinking, it is however, not as hard
as it sounds, because the wall/door/direction in witch i entered the
room doesnt need a state, so now we are at 2^5*6-1 = 191 and thats a
great deal lower than 255.

Good thinking.

So let's say you entered the room through the side 2:

Bit 0: side 0 = closed
Bit 1: side 1 = open
Bit 2: side 3 = closed //We've omitted side 2
Bit 3: side 4 = open
Bit 4: side 5 = closed

Now we've three further bits to specify by which side we entered the room.
(ie. 0 to 5)
Makes sense.
room doesnt need a state
However, with this aproach one problem remains.
The walls/doors no longer have a fixes position in the bit field, fx.
if the direction from witch the room was entered is 5 and therefor
wall/door no. 6 will be represented by bit no. 8 (as the 6th wall/door
doesnt need to be represented), if the direction ID is 4, the 5th
wall/door, will not be represented, it doesnt have to be. If the the
direction ID is 3 the 5th wall/door will be representet by the 7th bit,
ect.


Correct. The order has been disturbed.
Maybe that could give some inspiration.

Here's some code I just wrote. It's just to get you going; it may contain
errors and bugs as I wrote it quickly and sloppily.

class PresenceInRoom
{
private:

char data; //Stores our data

struct SixBools
{
bool side_status[6];
};
bool GetBit(unsigned i)
{
return i && ( 1U << (i - 1) );
}

SixBools GetSixBools()
{
//First we determine which side we came in through

unsigned side_came_in = 0;

if GetBit( 5 ) side_came_in += 4;
if GetBit( 6 ) side_came_in += 2;
if GetBit( 7 ) side_came_in += 1;

//Now we have our side

SixBools six_bools;

six_bools[side_came_in] = false;

{
unsigned bit = 0;

for (unsigned side = 0; side < 6; ++side, ++bit)
{
if (side == side_came_in)
{
--bit;
continue;
}

six_bools[side] = GetBit( bit );
}
}

}
};

-Tomás


looks nice, thank you wery much.
Im sry i havent got time to take a look at it right now,m but im on my
way to a party.
Regard

Feb 24 '06 #4

Tomás skrev:
posted:
imagine a hollow cube.
every wall can have one of two states: open or closed. This would take
6 bits to represent.
Six sides, each of two possible states, so 6 bits.

I agree.
Now, i allso neew to know whitch way i entered the room, this could be
representet with a integer from 0 to 5 = 3 bits.


There are six possible combinations for which side you used to enter the
room.

A 3-Bit number has eight possible combinations.

You're wasting the last two of the combinations.

As 2^6*6-1 = 383 and an 8 bit integer can hold a max value of 255, we
ofcourse have to do some creative thinking, it is however, not as hard
as it sounds, because the wall/door/direction in witch i entered the
room doesnt need a state, so now we are at 2^5*6-1 = 191 and thats a
great deal lower than 255.

Good thinking.

So let's say you entered the room through the side 2:

Bit 0: side 0 = closed
Bit 1: side 1 = open
Bit 2: side 3 = closed //We've omitted side 2
Bit 3: side 4 = open
Bit 4: side 5 = closed

Now we've three further bits to specify by which side we entered the room.
(ie. 0 to 5)
Makes sense.
room doesnt need a state
However, with this aproach one problem remains.
The walls/doors no longer have a fixes position in the bit field, fx.
if the direction from witch the room was entered is 5 and therefor
wall/door no. 6 will be represented by bit no. 8 (as the 6th wall/door
doesnt need to be represented), if the direction ID is 4, the 5th
wall/door, will not be represented, it doesnt have to be. If the the
direction ID is 3 the 5th wall/door will be representet by the 7th bit,
ect.


Correct. The order has been disturbed.
Maybe that could give some inspiration.

Here's some code I just wrote. It's just to get you going; it may contain
errors and bugs as I wrote it quickly and sloppily.

Some bugs indeed needed solving, however, its was nothing seriously.

class PresenceInRoom
{
private:

char data; //Stores our data

struct SixBools
{
bool side_status[6];
};
bool GetBit(unsigned i)
{
return i && ( 1U << (i - 1) );
}

SixBools GetSixBools()
{
//First we determine which side we came in through

unsigned side_came_in = 0;

if GetBit( 5 ) side_came_in += 4;
if GetBit( 6 ) side_came_in += 2;
if GetBit( 7 ) side_came_in += 1;

//Now we have our side

SixBools six_bools;

six_bools[side_came_in] = false;

{
unsigned bit = 0;

for (unsigned side = 0; side < 6; ++side, ++bit)
{
if (side == side_came_in)
{
--bit;
continue;
}

six_bools[side] = GetBit( bit );
}
}

}
};
Still, i dont quite understand how this is suposed to work

Regards

-Tomás


Feb 26 '06 #5

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

Similar topics

15
by: Tor Erik Sřnvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
9
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
8
by: Hahnemann | last post by:
Is there a sample in C out there to represent a sequence of numbers (shorts) using bits? With the intention of saving space, instead of using the full 16 bits per short. Thanks. - Hahnemann
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.