473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitwise ops on char[array] in c++

7 New Member
can anyone tell me if there is a way to apply bitwise operations, like & ^ | ~ << >> to an array of unsigned char as one contiguous block of bits?

ie. say i have

unsigned char value[5]
unsigned char mask[5]

is doing something like (value[] &= mask[]) reliable or well defined?

currently im implementing it as

for( i = 0 to 5 ) { value[i] &= mask[i] }

which isnt a problem, however if thats a loop i dont need, then it would be nice to drop it

moreso my functions for << and >> are pretty ugly and i know im losing the benefits of bitwise stuff with the way ive done it

thanks
Jul 1 '07 #1
2 5266
weaknessforcats
9,208 Recognized Expert Moderator Expert
If you are really using C++, then you should be using the bitset<> template from the C++ Standard LIbrary:

Expand|Select|Wrap|Line Numbers
  1. bitset<32>  mybits;       //creates 32 bits
  2. mybits.Set(15);             //set bit 15 to true
  3. mybits.Reset(8);           //reset bit 8 to false
  4. mybits.flip(5);              //flip true to false or false to true as the case may be
  5. mybits.test(20)            //returns true if bit 20 is true and false otherwise.
  6. etc...
  7.  
The use of C-style bitfields is not compatible with C++.
Jul 1 '07 #2
vusak
7 New Member
ive wrapped it all up nicely in class, i didnt find bitset flexible enough for my needs, im trying to maintain hardware and compiler independence, although im not too experienced with working on this kind of data type in this way

but if there is a way to perform bitwise ops across contiguous bits of memory, id love to know, ive been searching all over
Jul 1 '07 #3

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

Similar topics

4
11957
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
11
9830
by: Ger | last post by:
Hi, I have been trying to return a data packet (as a char array) at the end of a function but that seems to be impossible... I would like to use this char array in a different class..anyone some...
8
2732
by: ali | last post by:
Hi, I'm having a problem understanding the reason for output on the following code: #include <iostream.h> int main() {
3
1961
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
15
34566
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
15
435
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; ...
4
7039
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
13
6223
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
8
16234
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc =...
5
3773
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
7226
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
7388
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
5631
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,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
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...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.