bit-manipulation

Matrix transpose and population count

点点圈 提交于 2020-03-16 07:27:31
问题 I have a square boolean matrix M of size N, stored by rows and I want to count the number of bits set to 1 for each column. For instance for n=4: 1101 0101 0001 1001 M stored as { { 1,1,0,1}, {0,1,0,1}, {0,0,0,1}, {1,0,0,1} }; result = { 2, 2, 0, 4}; I can obviously transpose the matrix M into a matrix M' popcount each row of M'. Good algorithms exist for matrix transposition and popcounting through bit manipulation. My question is: would it be possible to "merge" such algorithms into a

What is wrong with print (2 & 2) >> 1?

假如想象 提交于 2020-03-03 10:00:07
问题 I am just wondering what happens with that piece of code. Why the result is incorrect only when printed directly, why is the newline ignored? user@host_09:22 AM: perl print 2 >> 1, "\n"; print 2 & 2, "\n"; print (2 & 2) >> 1, "\n"; 1 2 2user@host_09:22 AM: perl $a = (2 & 2) >> 1; print "$a\n"; 1 回答1: When you print it with warnings it becomes clear(er) perl -we'print (2 & 2), "\n"' says print (...) interpreted as function at -e line 1. Useless use of a constant ("\n") in void context at -e

what is the means of i &=(i-1) in java

一笑奈何 提交于 2020-02-08 02:58:52
问题 int n; for ( n = 0; i >0; n++) { i &= (i-1); } return n; //maybe its function is to count the number of 1, I don't know the sentence means 回答1: This is indeed counting the number of one-bits in the value of i. It does however only work properly for positive values. The JRE provided Integer.bitCount(i) does the same and it works in constant time as well as for negative values. As for how it works, its hard to understand if you're not familar with binary arithmetic. What basically happens that

Is there a way to encode any number into a series of 8-bit numbers, including a terminating character?

折月煮酒 提交于 2020-02-06 09:07:52
问题 So I would like to encode numbers as small as 0 and as high as very high (32-bit, 64-bit, other 8-bit multiples...). The simple approach is to just use the computer architecture's built-in support for "word" size or whatever, so like 32-bit or 64-bit are the common cases, so integers limited to that size. But I would like to do a theoretical thing and see if there is a way to encode arbitrarily large numbers using a sequence of 8-bit numbers. But then as a caveat, I want to know when we've

Counting number of set bits in a long number

十年热恋 提交于 2020-02-03 08:26:08
问题 This question has been answered here. My query is, following approach-1 works, however the variation of it, that is approach-2 does not, rather it gives double the value of expected output. I can not find out why. Approach-1 public class Solution { public int numSetBits(long a) { int count = 0; long temp = 0; for(int i = 0 ; i < 64 ; i++) { // 64-bit for long data-type temp = 1; temp = temp << i; temp = a & temp; if((temp > 0)) count++; } return count; } } Approach-2 public class Solution {

Finding next bigger number with same number of set bits

邮差的信 提交于 2020-02-02 07:41:27
问题 I'm working on a problem where I'm given a number n, I have to find the next larger element with same number of set bits. While searching on Internet, I found an interesting piece of code which does this in few lines of code (BIT MAGIC) here: unsigned nexthi_same_count_ones(unsigned a) { /* works for any word length */ unsigned c = (a & -a); unsigned r = a+c; return (((r ^ a) >> 2) / c) | r); } But I want to understand the underlying logic about the algorithm that it will work always. All the

Writing a stream of 9 bit values as bytes to a file in C

无人久伴 提交于 2020-01-30 09:05:20
问题 I have an array with integer values from 0-511 (9 bits max). I am trying to write this to a file with fwrite . For Example, with the array: [257, 258, 259] Which is 100000001, 100000010, 100000011 I am trying to write 100000001100000010100000011 + extra padding of 0s to the file But since fwrite limits writes to 1 byte at a time, I am not sure how to go about doing this. I am new to bitwise operations and am not how to separate out the individual bytes. 回答1: You need a bit buffer. Since you

C# Bitwise Operator With Ints

隐身守侯 提交于 2020-01-30 08:16:25
问题 What does this expression actually mean?? Note - the x and y vars are just sample values. int x = 3; int y = 1; if ((x & y) !=0) I inherited a codebase and am not up to speed on bitwise operators. I have read up, but am still missing something. Help! 回答1: It's comparing the bits in each value. It returns any bits that are set in both numbers. In your example: 3: 0011 1: 0001 3 & 1: 0001 回答2: This checks whether x and y both have at least one common bit set. In the case of your example this

Is there an objective-c specific way to count bits in an integer

◇◆丶佛笑我妖孽 提交于 2020-01-24 15:12:29
问题 I'd like to count the bits set to 1 in my 32-bit integer in objective-c. Some languages have this as a single call: Java has Integer.bitCount() C++ sometimes has __popcount() SQL has BIT_COUNT() Is there an equivalent for Objective-C? Otherwise I'll use : -(int32_t) BitCounter:(int32_t) v { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; } ...which is fine, but some processors have it as a single

Is there an objective-c specific way to count bits in an integer

随声附和 提交于 2020-01-24 15:12:08
问题 I'd like to count the bits set to 1 in my 32-bit integer in objective-c. Some languages have this as a single call: Java has Integer.bitCount() C++ sometimes has __popcount() SQL has BIT_COUNT() Is there an equivalent for Objective-C? Otherwise I'll use : -(int32_t) BitCounter:(int32_t) v { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return (((v + (v >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; } ...which is fine, but some processors have it as a single