Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

32
LINES

< > BotCompany Repo | #1036047 // countDifferingBits - between 2 byte, int or long arrays

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (73L/1K).

1  
static int countDifferingBits(byte[] array1, byte[] array2) {
2  
  int n = array1.length;
3  
  int diff = 0;
4  
  for i to n:
5  
    diff += Int.bitCount(ubyteToInt(array1[i])^ubyteToInt(array2[i]));
6  
  ret diff;
7  
}
8  
9  
static int countDifferingBits(int[] array1, int[] array2) {
10  
  int n = array1.length;
11  
  int diff = 0;
12  
  for i to n:
13  
    diff += Int.bitCount(array1[i]^array2[i]);
14  
  ret diff;
15  
}
16  
17  
static int countDifferingBits(long[] array1, long[] array2) {
18  
  int n = array1.length;
19  
  int diff = 0;
20  
  for i to n:
21  
    diff += Long.bitCount(array1[i]^array2[i]);
22  
  ret diff;
23  
}
24  
25  
// use sub-array of array2
26  
static int countDifferingBits(long[] array1, long[] array2, int i1) {
27  
  int n = array1.length;
28  
  int diff = 0;
29  
  for i to n:
30  
    diff += Long.bitCount(array1[i]^array2[i1+i]);
31  
  ret diff;
32  
}

Author comment

Began life as a copy of #1035863

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036047
Snippet name: countDifferingBits - between 2 byte, int or long arrays
Eternal ID of this version: #1036047/4
Text MD5: 23cfe248a6707ece5c899bc16f76da1d
Transpilation MD5: f033e894d7ce78c5a1dd0a9b9756c3f4
Author: stefan
Category: javax / bit ops
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-03 02:51:02
Source code size: 807 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 69 / 112
Version history: 3 change(s)
Referenced in: [show references]