bit-manipulation

Bit invert function in K&R exercise 2-7

送分小仙女□ 提交于 2019-12-23 12:42:06
问题 Exercise 2-7 of The C Programming Language : Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed to 0 and vice versa), leaving the others unchanged. I understood the question like this: I have 182 which is 101(101)10 in binary, the part in parentheses has to be inverted without changing the rest. The return value should be 10101010 then, which is 170 in decimal. Here is my attempt: #include <stdio.h> unsigned int getbits(unsigned

Is it possible to create a generic bitwise enumeration 'IsOptionSet()' method?

混江龙づ霸主 提交于 2019-12-23 12:04:37
问题 The below code makes it easy to pass in a set HtmlParserOptions and then check against a single option to see if it's been selected. [Flags] public enum HtmlParserOptions { NotifyOpeningTags = 1, NotifyClosingTags = 2, NotifyText = 4, NotifyEmptyText = 8 } private bool IsOptionSet(HtmlParserOptions options, HtmlParserOptions singleOption) { return (options & singleOption) == singleOption; } My question is, is it possible to create a Generic version of this (I'm guessing through implementing

Exclusive or between N bit sets

无人久伴 提交于 2019-12-23 10:59:06
问题 I am implementing a program in Java using BitSets and I am stuck in the following operation: Given N BitSets return a BitSet with 0 if there is more than 1 one in all the BitSets, and 1 otherwise As an example, suppose we have this 3 sets: 10010 01011 00111 11100 expected result For the following sets : 10010 01011 00111 10100 00101 01000 expected result I am trying to do this exclusive with bit wise operations, and I have realized that what I need is literally the exclusive or between all

Moving a bit within a byte using bitfield or bitwise operators

╄→гoц情女王★ 提交于 2019-12-23 09:50:55
问题 Is there an elegant way of moving a bit within a byte (or word/long). For simplicity, lets use a simple 8-bit byte and just one bit to move within the byte. Given a bit number, based on 0-7 Least-sig-bit to most-sig-bit, (or bits 1-8 if you'd rather), I would like to move a bit from one position to another: 7654 3210 <bit position 0101 1010 <some binary value --x- --y- <move bit from x to y 0111 0100 <new value with x moved to y and intervening bits shifted left So, x at bit position 5 moves

What's the best way to represent a short bit string?

*爱你&永不变心* 提交于 2019-12-23 09:27:40
问题 I want to represent a string of up to around 120 bits, and speed is critical. I need to be able to build a bitstring by repeated snoc operations, and then to consume it with repeated uncons operations. One idea is to steal the implementation of Word128 from data-dword and use something like this to build: empty = 1 snoc xs x = (xs `shiftL` 1) .|. x But the unconsing seems to get a bit ugly, having to first countLeadingZeros and shift left to eliminate them before being able to read off the

Bit shifting a character with wrap? C++

倖福魔咒の 提交于 2019-12-23 09:12:59
问题 I have a binary file that will be read in as characters. Each character was bit shifted to the left unknown number of times (assuming with wrap) by someone else. I want to be able to read in each character and then wrap shift to the right (the number of times to shift I guess will have to be figured out manually, because I haven't figured out another way). So, my current idea is that I read in a character, create a copy with temp and then use XOR: char letter; //will hold the read in letter

Weird output when summing 1<<2 and 1<<3 in C++

强颜欢笑 提交于 2019-12-23 08:39:08
问题 So I was just trying some bit manipulation in C++. Here is what I tried: int a = 1<<2; cout<<a; This gives the output as 4 . int a = 1<<3; cout<<a; This gives the output as 8 But when I do: int a = 1<<2 + 1<<3; cout<<a; It gives the output as 64 . Why so? I also tried: int a = 1<<2; int b = 1<<3; cout<<a + b; Which gives the output as 12 as expected. 回答1: This is because addition has a higher operator precedence than bitshift. In other words, your second example is equivalent to 1 << (2 + 1)

Can bitwise math be used for one-to-many relationships in SQL?

Deadly 提交于 2019-12-23 07:19:11
问题 Proper normalization in an RDBMS means a proliferation of tables. Integer fields can store orthogonal data as bits – can this be used as a substitute for an additional table, without sacrificing relational integrity? 回答1: The answer to your question is "no". The bit fields sacrifice relational integrity, for the simple reason that you have entities in the database that don't have a corresponding tables. That said, many databases offer support for this, generally through a "bit" data type.

Bitshift from a specific bit onwards in binary number? [duplicate]

不羁岁月 提交于 2019-12-23 07:08:57
问题 This question already has answers here : Command to pad zeros to specific locations in binary numbers? (5 answers) Closed 5 years ago . My idea for bit shifting binary numbers from many locations onwards is outlined below. I formulated this problem first as a zero-padding problem here but I am now starting to feel there may be an elegant bitshift solution. I can do bitshift such as bitshift(5,1) where 101 to 1010 but how to do it from a specific bit? For example, 101 from a certain bit

What is the most efficient way to zero all bits below the most significant set bit?

£可爱£侵袭症+ 提交于 2019-12-23 06:42:09
问题 So for the following sequence: 0001000111000 The desired result would be: 0001000000000 I am fully aware that this is achievable by finding the MSB's index with assembly BSRL (or similar bit-twiddling hack) then >> bit shifting the number by (index - 1), then << shifting back by (index - 1), but I want to know whether there is, specifically, an assembly instruction or a sequence of instructions with better performance, not a bit-twiddling hack that can do this. 回答1: There's no single