473,412 Members | 2,088 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,412 software developers and data experts.

comparing an array of bits

byenary
Hello
I have got 2 variabeles in an application the type;
typedef guint32 data[8]; -> so 256 bits
Now i have a load of data of this type derrived from fileheaders.
I want to compare these and get a number of how many bits are set to 1 on the same place.
So for Ex with 8 bits
data1 : 00110011
data2 : 00100010
This should give me 2 as an answer. I don't really know how to do this i am not that expririenced. I guess ORRING the whole array with the other or something like bitshifting is what i should do but i don't know how.

Thank you very much for any help!
Sep 24 '07 #1
2 2801
Banfa
9,065 Expert Mod 8TB
This should give me 2 as an answer. I don't really know how to do this i am not that expririenced. I guess ORRING the whole array with the other or something like bitshifting is what i should do but i don't know how.
XORing (^) the 2 values will give a result that has bits set where the bits in the 2 original values do not have the same value.

Having got this result count the number of bits set in it by, testing bit 0 shifting right 1 place and continuing while the result != 0.
Sep 24 '07 #2
made a little test ap wich does the job
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void show_binary(unsigned short u);
  4. void show_lastbit(unsigned short u);
  5. unsigned short count_ones(unsigned short test);
  6.  
  7. int main(void)
  8. {
  9.   unsigned short getal1;
  10.   unsigned short getal2;
  11.   getal1 = 14531;
  12.   getal2 = 14045;
  13.   show_binary(getal1);
  14.   show_binary(getal2);
  15.   unsigned short test = getal1 & getal2;
  16.   show_binary(test);
  17.   printf("aantal = %d",count_ones(test));
  18.   /*
  19.   0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 1
  20.   0 0 1 1 0 1 1 0 1 1 0 1 1 1 0 1
  21.   0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 1
  22.   1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 aantal = 5
  23.   */
  24.   return 0;
  25. }
  26.  
  27. unsigned short count_ones(unsigned short test){
  28.   int aantal = 0;   
  29.   int i=0;
  30.   for(i=0;i<sizeof(test)*8;i++){  
  31.       if(test & 1){ 
  32.           printf("1 ");
  33.           aantal++;
  34.           }
  35.       else 
  36.           printf("0 ");
  37.   test = test >> 1;      
  38.   }     
  39.     return aantal;
  40. }
  41.  
  42. void show_binary(unsigned short u)
  43. {
  44.   int n;
  45.  
  46.   for( n = 32768; n > 0; n = n / 2 )
  47.     if(u & n) 
  48.         printf("1 ");
  49.     else 
  50.         printf("0 ");
  51.  
  52.   printf("\n");
  53. }
  54.  
  55. void show_lastbit(unsigned short u)
  56. {
  57.   int n;
  58.     if(u & 1) 
  59.       printf("1 ");
  60.   else 
  61.       printf("0 ");
  62.  
  63.   printf("\n");
  64. }
Sep 24 '07 #3

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
6
by: sridhar | last post by:
#include <stdio.h> int main(){ unsigned int ui = 0; if(0x0ul <= ui){ printf("less eq\n"); } } On my system unsigned long is 64 bits and unsigned int is 32.The compiler gives a warning
4
by: Lance | last post by:
I have an array of bytes that I need to convert into an array of Integers. But, the number of bits per value in the Byte array is not necessarily divisible by 8 (although it will never exceed...
12
by: rajus | last post by:
I want to store the (x,y) coordinates of about 10,000 points in a 2D array.How can I store them and retrieve them later?
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
17
by: fgh.vbn.rty | last post by:
I know that std::vector<boolis specialized to store individual bools as single bits. I have a question about the comparison operation on a pair of vector<bool>s. Is the comparison done bit by...
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.