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

changing a single bit of an unsigned interger variable

Hi,
What i'm trying to write a code that would allow me change a single bit of a variable. So I've tried to store it in an array so that its possible for me to change a single bit of the variable by simply changing the value of the array index.
However this doesn't seem to work. See extract of code below.

Help please...ideas, suggestions?

Expand|Select|Wrap|Line Numbers
  1. typedef u_int32_t UInt32;
  2. UInt32 value = 0xFFFFFFFF;
  3. int index = 4;
  4.  
  5. UInt32* var = new UInt32[32];
  6. *var = value;
  7. var[index] = 0;
  8.  
  9. cout<<hex<<*chan<<endl;
  10.  
  11.  
Dec 9 '07 #1
5 3450
scruggsy
147 100+
Hi,
What i'm trying to write a code that would allow me change a single bit of a variable. So I've tried to store it in an array so that its possible for me to change a single bit of the variable by simply changing the value of the array index.
However this doesn't seem to work. See extract of code below.

Help please...ideas, suggestions?

Expand|Select|Wrap|Line Numbers
  1. typedef u_int32_t UInt32;
  2. UInt32 value = 0xFFFFFFFF;
  3. int index = 4;
  4.  
  5. UInt32* var = new UInt32[32];
  6. *var = value;
  7. var[index] = 0;
  8.  
  9. cout<<hex<<*chan<<endl;
  10.  
  11.  
That's not going to work.
To manipulate individual bits you can use the bit-wise operators or a bitset. You should probably learn how the bit-wise operators work since that is rather fundamental to an understanding of how computer store and manipulate data.
Examples in binary:
0001 | 0010 = 0011
0111 & 1001 = 0001
~0101 = 1010
Dec 9 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You should probably learn how the bit-wise operators work
Actually, for C++ you should learn the bitset<> template. The bitwise operators are very low level and are rarely used in C++ unless the C++ is being written by a C programmer.
Dec 9 '07 #3
That's not going to work.
To manipulate individual bits you can use the bit-wise operators or a bitset. You should probably learn how the bit-wise operators work since that is rather fundamental to an understanding of how computer store and manipulate data.
Examples in binary:
0001 | 0010 = 0011
0111 & 1001 = 0001
~0101 = 1010
Thanks. But my reluctance to masking is that there are 32 possible values for index. So that would mean writing 32 possible conditions. Isn't there a shorter way of doing this?
Dec 11 '07 #4
scruggsy
147 100+
Thanks. But my reluctance to masking is that there are 32 possible values for index. So that would mean writing 32 possible conditions. Isn't there a shorter way of doing this?
Yes, use the left shift operator.
More examples in binary:
1 << 0 = 1
1 << 1 = 10
1 << 2 = 100 etc...

This means all you need to know is the position of the bit (0 thru 31) you want to change. Use the shift operator to get your mask, then & or | to flip your bit.
Dec 11 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Again, I say: Use a bitset.

Expand|Select|Wrap|Line Numbers
  1. bitset<32>  thebits;
  2. thebits.set(19);                  //bit 19 is 1
  3. thebits.reset(19);                //bit 19 is 0
  4. thebits.test(19);                  //true if bit 19 is 1
  5. //etc
  6.  
You can use an enum to name your bits:
Expand|Select|Wrap|Line Numbers
  1. enum bits {POWER_ON, PAPER_LOADED, TONER_LOW};
  2.  
then use the enum names instead of the bit number.

Those bitwise operators are supposed to remain in C.
Dec 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
5
by: Daniel Rudy | last post by:
How does one covert a interger number in a unsigned long long int (64-bit) to long double (80-bit) storage? I looked at math.h and I found function that convert double to long long, but didn't...
10
by: Altman | last post by:
I have only done a little programming in C++ so I am still learning but I am having a problem with a variable that is changing on me. I have tried this 2 ways with the same result. I have a...
26
by: LuB | last post by:
This isn't a C++ question per se ... but rather, I'm posting this bcs I want the answer from a C++ language perspective. Hope that makes sense. I was reading Peter van der Linden's "Expert C...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
1
by: Veeves | last post by:
I would like to change the width of a datagrid column at run time. I have noticed when creating a datagrid, if the header text is shorter than the data in the column, then the column width set by...
1
by: DUMMY Keyword in SQL Server 2000 | last post by:
I have a variable declared as integer which I populate based on the value on a grid. This value can be NULL. However, I get an exception when I try to save this value to the database even though...
6
by: Kris | last post by:
Hi guys, been thinking on this for a while and cant see a solution. How can you dynamically change the base type of a pointer? Say I have to structs conveying the same information but relate to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.