bitwise

OR-ing bytes in C# gives int

I have this code. byte dup = 0; Encoding.ASCII.GetString(new byte[] { (0x80 | dup) }); When I try to compile I get: Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) Why does this happen? Shouldn't | two bytes give a byte? Both of the following work, assuring that each i...

Why do we always declare constants to be powers of 2?

Most of the constants I see in others code are powers of 2, i.e. #define SIZE 256 or public static final int SIZE = 2048; Is there any particular reason why we do that instead of i.e. #define SIZE 257 ? Thanks in advance ...

Bit shifting N bits

Hello quick question regarding bit shifting I have a value in HEX = new byte[] { 0x56, 0xAF }; which is 0101 0110 1010 1111 i want to the first n bits, example 12 then shift off the remaining 4 (16-12) to get 0000 0101 0110 1010 (1386 dec) i cant wrap my head around it and make it scalable for n bits. Thanks! ...

Algorithm for copying N bits at arbitrary position from one int to another

An interesting problem I've been pondering the past few days is how to copy one integer's bits into another integer at a given position in the destination integer. So, for example, given the destination integer 0xdeadbeef and the source integer 0xabcd, the idea would be to get a result of 0xabcdbeef (given a destination position of 16 bi...

Flags enum & bitwise operations vs. “string of bits”

A fellow developer suggested we store a selection of days of the week as 7-character string of 1’s and 0’s, i.e. “1000100” for Monday and Friday. I preferred (and strongly suggested) a solution with a Flags enum and bitwise operations, I think it's a cleaner way of doing this, and it should be easier to understand for other developers. ...

Help me improve this C++ bit-buffer processing code

I am writing a function to process an incoming 32-bit buffer, representing changing data when it is compared to an corresponding stored 32-bit buffer. The position of the changing bit represents a number (i.e. a value of 8 means bit 3) that needs to be processed, along with whether the change is 0->1, or 1->0. Here is the current impleme...

Why should I use bitwise/bitmask in PHP?

I am working on a user-role / permission system in PHP for a script. Below is a code using a bitmask method for permissions that I found on phpbuilder.com. Below that part is a much simpler version w3hich could do basicly the same thing without the bit part. Many people have recommended using bit operators and such for settings and ...

storing a value from an unsigned interger (2bytes long) to an unsigned char variable using bitwise operators?

How do I put the value of 0x04 in register 4 if the instruction was 1rxy? 1RXY-Load register R with the value at memory address XY #include <stdio.h> unsigned char r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,ra,rb,rc,rd,re,rf; void reg_check(unsigned char reg); void rxy1(unsigned char reg, unsigned char val); int main(){ unsigned char memloc1=...

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 understanding the exact thing they're looking for me...

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

Bitwise operations: online resources?

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

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

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

Hi, i'm need to calculate net mask and subnet addr using broadcast addr and host address from this subnet. I must use only bitwise operations not comparing of string representation, sysadmin tools and so on. I have some formulas for calculating addresses. but i dont know, how using it with my source data. ^ -- is bitwise xor ~ -- ne...

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

Bitwise operators LinqDataSource Where

Hi, 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 == (Type | 1) Type = (Type | 1) Type | 1 ...

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

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 etc.... It looks like there be some bitwise logic that may ...

C# (C++) 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 been in a reason to use them. I've been studying how they work; example below shows the shift bitwise operator. Could anyone furthur explai...

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' way of doing it: v = ord(data[17]) ...

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 values assigned to the permission object....