4
голосов
3ответов
1229 просмотров

Bit shifting in internet checksums

This is almost certainly a very silly question, but for some reason I'm having trouble with internet checksum calculations. All of the algorithms basically look something like this: WORD chksm(WORD *startpos, WORD checklen){ ulong sum = 0; WORD answer = 0; while (checklen > 1) { sum += ...

1
голосов
3ответов
503 просмотров

Bitwise enum cast return value not expected

I have the following enum: [Flags] public enum PermissionLevel { User = 1, Administrator = 2, ITStaff = 3, Manager = 4, SuperAdministrator = 6, } When I do: PermissionLevel permission = (PermissionLevel) dr.GetInt32(i); I get random permission...

1
голосов
4ответов
593 просмотров

what is the correct way to process 4 bits inside an octet in python

I'm writing an application to parse certain network packets. A packet field contains the protocol version number in an octet, so that 4 high bits are the 'major' and low 4 are the 'minor' version. Currently I am parsing them as follows, but am wondering if there is a prettier or more 'pythonic' w...

4
голосов
12ответов
9350 просмотров

Using bitwise operators

I've been studying C# and ran accross some familiar ground from my old work in C++. I never understood the reason for bitwise operators in a real application. I've never used them and have never had in a reason to use them. I've been studying how they work; the example below shows the shift bitwi...

4
голосов
5ответов
4260 просмотров

Comparing arbitrary bit sequences in a byte array in c

I have a couple uint8_t arrays in my c code, and I'd like to compare an arbitrary sequence bits from one with another. So for example, I have bitarray_1 and bitarray_2, and I'd like to compare bits 13 - 47 from bitarray_1 with bits 5-39 of bitarray_2. What is the most efficient way to do this? ...

0
голосов
3ответов
766 просмотров

Best way to merge hex strings in c++? [heavily edited]

I have two hex strings, accompanied by masks, that I would like to merge into a single string value/mask pair. The strings may have bytes that overlap but after applying masks, no overlapping bits should contradict what the value of that bit must be, i.e. value1 = 0x0A mask1 = 0xFE and value2 = ...

1
голосов
2ответов
143 просмотров

What is being done in this bit shifting operation?

(INBuffer[3] << 8) + INBuffer[2] Is this essentially moving the bit in INBuffer[3] into INBuffer[2] or [3] being zero'ed out then added to [2]?

8
голосов
4ответов
7035 просмотров

Calculating Highest Power of 2 That Evenly Divides a Number in C

I need to write some logic to determine, given an even number. The highest power of two that evenly divides it. What is the maximum value of 2^n where Input % 2^n == 0? IE: Input -> Output 4 (0100) -> 4 8 (1000) -> 8 12 (1100) -> 4 14 (1110) -> 2 24 (11000) -> 8 ...

6
голосов
2ответов
4175 просмотров

What does the bitwise code "$n & ($n - 1)" do?

What does this code mean and what are other ways accomplish the same without using bit shifting? if ($n & ($n - 1))

3
голосов
2ответов
4034 просмотров

How can I XOR these base64'd numbers?

I have are 5 binary strings base64_encode()'ed wAD4Af8B/gHuA/4BzgP1A/8P/h//f/xv+z30D9IDSAE= AAAgCPgf/B/4H/w1+B74Gfg/+B/8P/4f/D/8HwABAAA= AAAAAMB/wP/A/8B/4HvAf+B/+n/3P/Y//z/4n4CDgAE= AAAAXcB/wH/Af8B/wHfAP+B/6H/xf+7//r/4f0CngFY= AAiwifAP+B/4D/gf8B74D/gd8V/4H/gP8B/8vwABAAA= AAAAAAAA/QD/Af4B/iP...

65
голосов
11ответов
32299 просмотров

What USEFUL bitwise operator code tricks should a developer know about?

I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more efficiently?

2
голосов
8ответов
1051 просмотров

Counting common bits in a sequence of unsigned longs

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 = ...

0
голосов
3ответов
591 просмотров

How do I determine the position of the nth most significand bit set in a 64-bit value?

I'm using some long values as bitmaps in a Java program. Here's my method so far: public class BitmapUtil { private static final long _B_MASK_LEFT_ON = 0x8000000000000000L; public static long setNthMsb(int n) { return BitmapUtil._B_MASK_LEFT_ON >>> n; } pub...

85
голосов
7ответов
73544 просмотров

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { unsigned int version : 3; unsigned int type :...

9
голосов
7ответов
6182 просмотров

Is a bit field any more efficient (computationally) than masking bits and extracting the data by hand?

I have a numerous small pieces of data that I want to be able to shove into one larger data type. Let's say that, hypothetically, this is a date and time. The obvious method is via a bit field like this. struct dt { unsigned long minute :6; unsigned long hour :5; unsigned long day :5...

6
голосов
7ответов
4391 просмотров

Removing lowest order bit

Given a binary number, what is the fastest way of removing the lowest order bit? 01001001010 -> 01001001000 It would be used in code to iterate over the bits of a variable. Pseudo-code follows. while(bits != 0){ index = getIndexOfLowestOrderBit(bits); doSomething(index); removeLowest...

0
голосов
1ответов
321 просмотров

Bitwise operators LinqDataSource Where

I have a byte column called 'Type' in my MS Server database. On my Asp.net page I have a ListView and a LinqDataSource. The 'Type' column is different enum flags. I would like to check specific bits in column 'Type' in my LinqDataSource Where property. I tried with: (Check first bit) Type == (...

2
голосов
6ответов
9321 просмотров

Right shift (Division) -> ROUND TOWARD ZERO

I am doing this.. value >> 3; It is always going toward negative side.How do I round toward zero with right shift division?

1
голосов
2ответов
708 просмотров

Manipulating Nybbles and smaller in c#

I have an application that requires manipulating nybbles and possibly even individual bits at a time. Is there a library in C# that can help me?

0
голосов
2ответов
1290 просмотров

Calculate net mask and subnet address from broadcast and address inside subnet

I need to calculate a net mask and subnet addr using broadcast addr and host address from this subnet. I must use only bitwise operations and not comparing of string representation, sysadmin tools and so on. I have some formulas for calculating addresses. But I don't know how to use it with my ...

15
голосов
7ответов
9597 просмотров

number of 1's in 32 bit number

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.

4
голосов
3ответов
2569 просмотров

(Bitwise) Supersets and Subsets in MySQL

Are the following queries effective in MySQL: SELECT * FROM table WHERE field & number = number; # to find values with superset of number's bits SELECT * FROM table WHERE field | number = number; # to find values with subset of number's bits ...if an index for the field has been created...

6
голосов
6ответов
517 просмотров

Is there any advantage to using '<<1' instead of '* 2'?

I've seen this a couple of times, but it seems to me that using the bitwise shift left hinders readability. Why is it used? Is it faster than just multiplying by 2?

65
голосов
4ответов
44135 просмотров

How do you set, clear and toggle a single bit in JavaScript?

How to set, clear, toggle and check a bit in JavaScript?

5
голосов
2ответов
2304 просмотров

A C preprocessor macro to pack bitfields into a byte?

I'm getting into micro-controller hacking and while I'm very comfortable with bitwise operators and talking right to the hardware, I'm finding the resulting code very verbose and boilerplate. The higher level programmer in me wants to find an effective but efficient way to clean it up. For inst...

3
голосов
1ответов
624 просмотров

Bitwise operations: online resources?

Is there a nice e-book (preferably PDF, like Array and Pointers in C) explaining bitwise operations? Other resources also welcome, but I'm after a self-contained document, if possible. Thanks

3
голосов
12ответов
626 просмотров

visiting all free slots in a bitfield

I have an array of uint64 and for all unset bits (0s), I do some evaluations. The evaluations are not terribly expensive, but very few bits are unset. Profiling says that I spend a lot of time in the finding-the-next-unset-bit logic. Is there a faster way (on a Core2duo)? My current code can...

9
голосов
2ответов
21591 просмотров

How to bitwise shift in VB.NET?

How do I bitwise shift right/left in VB.NET? Does it even have operators for this, or do I have to use some utility method?

4
голосов
7ответов
8420 просмотров

C - Need to compare `n` lowest bits of an int for equality

C - Need to compare n lowest bits of an int for equality. I.e. n = 4; xxxx1001 == xxxx1001 (x is don't care) I.e. n = 2; xxxxxx01 == xxxxxx01 Can't think of a nice way to do it without using masks, =).

7
голосов
6ответов
1881 просмотров

K&R C Exercise Help

I've been going through the K&R C Programming Language book and I'm stuck on Exercise 2-6 which reads: Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. I'm having trouble unders...