bit-manipulation

Where to learn about “bit”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 14:31:25
问题 I am trying to find some books or resources talking about bit in detail so that for example I would be able to translate a number (like 16) into bits. I am currently a high school student and whenever reading a programming books I can understand almost everything except the bit/bitwise operators part. I just do not know how it works and why do people even invent bit & byte :(. Therefore, I hope that you guys can give me some resources suggestions talking about how to translate number

Outputting bit data to binary file C++

老子叫甜甜 提交于 2019-12-10 14:14:17
问题 I am writing a compression program, and need to write bit data to a binary file using c++. If anyone could advise on the write statement, or a website with advice, I would be very grateful. Apologies if this is a simple or confusing question, I am struggling to find answers on web. 回答1: Collect the bits into whole bytes, such as an unsigned char or std::bitset (where the bitset size is a multiple of CHAR_BIT), then write whole bytes at a time. Computers "deal with bits", but the available

Bitwise operators in java only for integer and long?

♀尐吖头ヾ 提交于 2019-12-10 14:02:22
问题 I wrote following code in Eclipse: byte b = 10; /* some other operations */ b = ~b; Eclipse wanted a cast to byte in the line of the bitwise complement. It said: "Type mismatch: cannot convert from int to byte". I also tried this with other bitwise operations and on other integral types. It was with short and char the same. Only long and integer could use bitwise operations. Is there a reason for this? 回答1: Unary (such as ~ ) and binary operators in Java subject their operands to "unary

Replicating Javascript bitwise operation in Python

瘦欲@ 提交于 2019-12-10 13:55:21
问题 I'm trying to replicate a simple bitwise Javascript operation in Python. [Javascript] > 0xA867Df55 2825379669 > 0xA867Df55 ^ 0 -1469587627 [Python] >>> 0xA867DF55 2825379669L >>> 0xA867DF55 ^ 0 2825379669L Having read the following: Bitwise OR in ruby vs javascript it sounds like the issue here is that 0xA867Df55 (2825379669) in Javascript is larger than the largest signed 32-bit int (2147483647), which is causing an unexpected Javascript result. The mail then goes on to suggest: "If for some

Bitwise right shift >> in Objective-C

此生再无相见时 提交于 2019-12-10 13:49:55
问题 I have a variable (unsigned int) part_1 . If I do this: NSLog(@"%u %08x", part_1, part_1); (print unsigned value, and hex value) it outputs: 2063597568 7b000000 (only first two will have values). I want to convert this to 0000007b So i've tried doing unsigned int part_1b = part_1 >> 6 (and lots of variations) But this outputs: 32243712 01ec0000 Where am i going wrong? 回答1: You want to shift by 6*4 = 24 bits, not just 6 bits. Each '0' in the hex printf represents 4 bits. unsigned int part_1b =

Converting java method to C#: converting bytes to integers with bit shift operators

☆樱花仙子☆ 提交于 2019-12-10 13:45:23
问题 I am trying to convert the following 2 methods into c# without the .net compiler complaining at me. Quite frankly I just don't understand how the two methods are really working behind the scenes. So an answer and explanation would be great here. public static int bytesToInt(byte b0, byte b1, byte b2, byte b3) { return (((int)b0 << 24) & 0xFF000000) | (((int)b1 << 16) & 0x00FF0000) | (((int)b2 << 8) & 0x0000FF00) | ((int)b3 & 0x000000FF); } public static byte[] charToBytes(char c) { byte[]

Finding N contiguous zero bits in an integer to the left of the MSB position of another integer

…衆ロ難τιáo~ 提交于 2019-12-10 12:57:26
问题 The problem is: given an integer val1 find the position of the highest bit set (Most Significant Bit) then, given a second integer val2 find a contiguous region of unset bits to the left of the position yielded from the first integer. width specifies the minimum number of unset bits that must be found in contiguity (ie width zeros without ones within them). Here is the C code for my solution: #include <limits.h> /* for CHAR_BIT - number of bits in a char */ typedef unsigned int t; unsigned

bitwise operations between elements in a list

匆匆过客 提交于 2019-12-10 12:47:31
问题 I have a of list of bitwise elements, e.g. [1,1,1], and I want to do a bitwise OR operation between every element in the list. So, e.g. for [1,1,1] do 1 | 1 | 1 = 1 or for [1,17,1] do 1 | 17 | 1 = 17 How can I do this without looping? Numpy's bitwise_or only seems to work on 2 arrays. Is there a bitwise & or | that works on every element, similar to sum, or np.mean? Thanks. 回答1: This works for numpy reduce: >>> ar = numpy.array([1,17,1]) >>> numpy.bitwise_or.reduce(ar) 17 回答2: You can use

number of 1's in 32 bit number

别来无恙 提交于 2019-12-10 12:42:06
问题 I am lookin for a method to have number of 1's in 32 bit number without using a loop in between. can any body help me and provide me the code or algorithm to do so. Thanks in advance. 回答1: See Integer.bitCount(int). You can refer to the source code if you want to see how it works; many of the Integer class's bit twiddling routines are taken from Hacker's Delight. 回答2: See the canonical reference: Bit Twiddling Hacks 回答3: Short, obscenely optimized answer (in C): int pop(unsigned x) { x = x -

Counting common bits in a sequence of unsigned longs

巧了我就是萌 提交于 2019-12-10 11:18:33
问题 I am looking for a faster algorithm than the below for the following. Given a sequence of 64-bit unsigned integers, return a count of the number of times each of the sixty-four bits is set in the sequence. Example: 4608 = 0000000000000000000000000000000000000000000000000001001000000000 4097 = 0000000000000000000000000000000000000000000000000001000000000001 2048 = 0000000000000000000000000000000000000000000000000000100000000000 counts