GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Aug-2006, 03:56
axel2500 axel2500 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
axel2500 is on a distinguished road

bit operations on a string


hy there.

i have a small, but not so hard question for you

Say i have
char temp[4096];

what i want to do is a procedure that does this:
bit (FALSE, 6, temp[x]);
Explanation:
A bit can have two values: TRUE (1) and FALSE (0).
array temp[] has 4096 elements.
one element represents one byte.
one byte has 8 bits.
i want to manipulate the bits within one element of array "temp[]".
If a make a call "bit (FALSE, 6, temp[x]);" the function does the following:
modify bit 6 to FALSE in element x of array temp[].
That's all.

Thanks.
  #2  
Old 20-Aug-2006, 08:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bit operations on a string


Quote:
Originally Posted by axel2500
hy there.

i have a small, but not so hard question for you

I don't see a question anywhere in your post.



Here are some examples of questions:

A very general compound question (a question with a followup that assumes the first part has a non-null answer):

1. What have you tried? Can you show us?


Here is an example of a specific question:

2. Do you know how to set a given bit to '1' in a given char (without changing the other bits)?


Here is another question:

3. Do you know how to reset a given bit to '0' in a given char (without changing any of the other bits)?


Here is another question:

4. Do you know how to create a function that changes the value of one of its arguments?


Here is another question, with two-part exclusive-or followup

5. Have you tried any of these things?

If you have, then

5a. Will you show us what you tried?

If not, then

5b(i). Have you read about the bit operators '&' and '|'?
5b(ii). Have you read about functions and passing values back and forth from a calling program to a function?


Finally:

6. What kind of help would you like for someone to give you?

Regards,

Dave
  #3  
Old 20-Aug-2006, 11:19
axel2500 axel2500 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
axel2500 is on a distinguished road

Re: bit operations on a string


I see I haven't been so explicit. My bad.

The question was: how a function called "bit" would look like (provided it does what i have written earlier).
I have not used binary operators before.
I only want a single bit to be modified, and the other bits to be left alone (the bits of a char).
The function is self explanatory in my opinion.
The arguments passed to the function "bit" are bool, int, and an array of chars.


Example:
char temp[0]= |1|1|1|1|0|1|0|0|
bit (FALSE, 3, temp[0]); // modify char temp[0]'s third bit to FALSE or zero
the array is modified as follows (actually only element [0] of array temp[] changes):
temp[0]= |1|1|0|1|0|1|0|0| (the third "1" has now become "0" - we made it a zero (the FALSE argument in the function call)) remember, only element 0 from array temp has been modified, the rest of the elements are unchanged.

now...
bit (TRUE, 1, temp[0]);
the temp[0] looks now like this:
temp[0]= |1|1|1|1|0|1|0|0|
as you can see, the function bit tells char temp[0] to change it's first bit to "1", but it was already "1", so it remains unchanged:
temp[0]= |1|1|1|1|0|1|0|0|

That pretty much covers my example.
I hope I got it right this time.

Regards.
  #4  
Old 20-Aug-2006, 11:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bit operations on a string


Quote:
Originally Posted by axel2500
I see I haven't been so explicit.
OK. Now, let's start
Quote:
Originally Posted by axel2500
The question was: how a function called "bit" would look like

Well, since you haven't used the bit operators that I asked about ('&' and '|'), the first thing to do is to examine their operation.

For operating on two single bits (we get to entire chars a little later):

The operation is sometimes described by a truth table. Now I am going to show truth tables where values are zero and one, since that is how C programs use them. You can see why C programmers usually define "false" to be zero and "tru" to be one, after a little discussion.

Anyhow, here is how the C operators work on individual bits:

Code:
======================================================= ======================================================= Truth table for the '|' operator operating on one bit Operand 1 Operand 2 Operand 1 | Operand 2 ------------------------------------------------ 0 0 0 0 1 1 1 0 1 1 1 1 ======================================================= ======================================================= Truth table for the '&' operator operating on one bit Operand 1 Operand 2 Operand 1 & Operand 2 ------------------------------------------------ 0 0 0 0 1 0 1 0 0 1 1 1 ======================================================= =======================================================


Suppose the user input is operand 1 we are going to feed it into a "process", and we want to force the output of the "process" to be have a logic value of 1. We will operate on the user value with our operand (number 2) and the output of the process will be the value of the operation (the third column).

Now, what if we have one bit from the user and we want to make sure that bit is set to a value of one? Note the second and fourth lines of the '|' table. If we perform the '|' operation with our operand value equal to one, then no matter what the user input value is, the output value will be one. Note the first and third lines: if we perform the '|' operation with our operand value equal to zero, the output value will be the same as the user input value.

Since the '|' operates on all of the bits of its operands simultaneously, the bottom line is:
If we want to set a bit to one, no matter what the user input bit is, we perform a '|' operation with "something" that has a one in that bit position.

If we want to leave a bit exactly the same as the user input, then we can perform a '|' operation with "something" that has a zero in that bit position.

The "something" is typically referred to as a "mask"

What if we want to make sure the output of our process is zero, no matter what the user value is? Look at the first and third of the '&' table, and you see that we can always make the output zero by perform an '&' operation with a value of zero. Note, further that if we perform a '&' operation with "something" that has a value of one, the output will be exactly the same as the input
So, here's a question for you:

How can we use the '&' operator to work on a user input char in such a way that the output will have a bit forced to be zero (no matter what the user input was) and will have all the other bits the same as the user bits?

How do we use this in your program?

(I assume that chars are eight bits, which they almost certainly are, but the method can be used in integer data types of any size.)

Bit numbering is usually as follows:

Bit 0 is the lsb (least significant bit) of a char (the right-most bit of a binary display).

Bit 1 is the next bit up from the lsb.
.
.
.
Bit 7 is the msb (most significant bit).

Now, suppose we want a program that gets a char from the user (by some method) and we want to create an output that has the least significant bit set to one but has all of the other bits the same as the user input. Here's a way:

CPP / C++ / C Code:
    unsigned char inchar;        /* the input char  */
    unsigned char outchar;      /* the output char  */
    unsigned char mask = 0x01; /* used with '|' to set the lsb */
.
.
/* get the input character somehow */
.
    outchar = inchar | mask;
.
.
.

That's it.

.



In general, our mask can have any number of bits equal to 1. Output bits in those positions will be equal to 1, whereas output bits corresponding to zero bits in our mask will have exactly the same value as that bit of the input datum.


To do the "set to True" part of your program, your function will have to come up with a way to create a mask with a bit value of 1 in a particular position and a value of zero for the other bits.

My suggestion is to do the program one thing at a time:

Get user input of a single byte (Initial byte value and bit that is to be set)
Write a function that takes the user byte and bit that sets that bit of that byte.
Test the code with lots of different input bytes and bits.

When the "set to true" stuff is working perfectly, then attack the "reset to false" part in a similar manner.

etc.



Regards,

Dave
  #5  
Old 21-Aug-2006, 05:30
axel2500 axel2500 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
axel2500 is on a distinguished road

Re: bit operations on a string


I think i've got it.

here is the code that does what you explained to me.

Code:
#include <stdio.h> #include <iostream> unsigned char temp=0; //binary 0000 0000 int main () { printf ("element is: %i\n", temp); unsigned char mask = 128; // binary 1000 0000 temp = temp | mask; printf ("element now is: %i\n", temp); mask = 64; // binary 0100 0000 temp = temp | mask; printf ("element now is: %i\n", temp); //element becomes 1100 0000.... getchar(); }

This sets the bits to TRUE.
The FALSE is done in a similar manner like you said Dave.

The output is:
C:\Dev-Cpp>Untitled1.exe
element is: 0
element now is: 128
element now is: 192

I've got the writing to a specific location in the char figured out.

How do i read?
How do i know that bit 4 within char "temp" is 1 or 0?
What about bit 1, or 5, or 7 ?
Is there an easy way?
I need to know how to read any bit inside a char.

The second function would be:
read_bit (5, temp[0]);
This function would read the fifth bit of element 0 belonging to array temp.
Please, point me in the right direction.


the first table ot truth resembles that of logical OR, and the second one of logical AND. I think.

Thanks.
  #6  
Old 21-Aug-2006, 07:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,709
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bit operations on a string


Quote:
Originally Posted by axel2500
I think i've got it.

How do i read?
How do i know that bit 4 within char "temp" is 1 or 0?
What about bit 1, or 5, or 7 ?


Perform the '&' with a mask that has 1 in the position of interest and zeros everywhere else. Then all of the other bits will be zero, and the value of the masked bit will be whatever that bit was in the thing you are testing.

Bottom line: The result will be zero if that bit was zero and will be not zero if that bit was one. So, just compare the result with 0:

CPP / C++ / C Code:
    mask = 0x02; /* 00000010 binary for testing bit 1 */
    temp = 123;
.
.
.
    if ((mask & temp) != 0) {
        printf("there is a 1 in the bit position\n");
    }
    else {
        printf("there is a 0 in the bit position\n");
    }


Regards,

Dave
  #7  
Old 21-Aug-2006, 15:52
axel2500 axel2500 is offline
New Member
 
Join Date: Aug 2006
Posts: 6
axel2500 is on a distinguished road

Re: bit operations on a string


Well Dave, that pretty much covers it.
It seems like i have my answers.

Thank you.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 07:56
Need help with strings sasukekun C++ Forum 4 24-Apr-2006 10:51
variables return to previous value after i try to set them nasaiya MS Visual C++ / MFC Forum 2 14-Jun-2005 00:43
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Converting string to float CT++ C++ Forum 2 10-May-2005 11:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.