bit-manipulation

Set individual bit in C++

Hi, I have a 5 byte data element and I need some help in figuring out how in C++ to set an individual bit of one of these byte; Please see my sample code below: char m_TxBuf[4]; I would like to set bit 2 to high of byte m_TxBuf[1]. 00000 0 00 ^ This one Any support is greatly appreciated; Thanks! ...

How to de-interleave bits (UnMortonizing?)

What is the most efficient way to de-interleave bits from a 32 bit int? For this particular case, I'm only concerned about the odd bits, although I'm sure it's simple to generalize any solution to both sets. For example, I want to convert 0b01000101 into 0b1011. What's the quickest way? EDIT: In this application, I can guarantee t...

question about swap bits

i have question on this topic i think that answer is incorrect look http://stackoverflow.com/questions/1192487/swap-bits-in-a-number-in-c 1110 1011 it is equal 235 but i get 3051 why? ...

Finding Bit Positions in an unsigned 32-bit integer

I think I might have been asleep in my CS class when they talked about Bit Positions, so I am hoping someone can lend a hand. I have a unsigned 32-bit integer (Lets use the value: 28) According to some documentation I am going over, the value of the integer contains flags specifying various things. Bit positions within the flag are nu...

Is using enum for integer bit oriented operations in C++ realiable/safe?

Consider the following (simplified) code: enum eTestMode { TM_BASIC = 1, // 1 << 0 TM_ADV_1 = 1 << 1, TM_ADV_2 = 1 << 2 }; ... int m_iTestMode; // a "bit field" bool isSet( eTestMode tsm ) { return ( (m_iTestMode & tsm) == tsm ); } void setTestMode( eTestMode tsm ) { m_iTestMode |= tsm...

why is 1>>32 == 1?

I'm wondering if perhaps this is a JVM bug? java version "1.6.0_0" OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu13) OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode) class Tmp { public static void main(String[] args) { System.out.println("1>>1 = "+(1>>1)); System.out.println("1>>2 = "+(1>>2));...

From a given number, set a var to -1 if negarive and to 1 if positive (without using if)

I often need this kind of function, for example, for understand the direction of the touches on iPhone and the only way to solve this problem it's by using logic, like this: int dir,distY; distY = newY-oldY; if (distY > 0) { dir = 1; } else if (distY < 0) { dir = -1; } I would like to know if there is an way to do it in one...

characters XOR with caret manipulation

Working with exclusive-OR on bits is something which is clear to me. But here, XOR is working on individual characters. So does this mean the byte which makes up the character is being XORed? What does this look like? #include <iostream.h> int main() { char string[11]="A nice cat"; char key[11]="ABCDEFGHIJ"; for(int x=0; x<10; ...

VB.NET Bit manipulation: how to extract byte from short?

Given this Short (signed): &Hxxxx I want to: Extract the most right &HxxFF as SByte (signed) Extract the left &H7Fxx as Byte (unsigned) Identify if the most left &H8xxx is positive or negative (bool result) ...

How do you properly void a permission when using Bitwise Permissions?

Alright, Here's the gist of what I'm planning to do. I'm going to have two tables. One with "ranks" or "roles" and one with users. I want to assign permissions on a role/user basis and retract permissions on a user basis. So, just for general purposes lets say that we have $role_can $user_can and $user_cant I know that for specifyin...

What is CHAR_BIT?

From http://graphics.stanford.edu/~seander/bithacks.html: int v; // we want to find the absolute value of v unsigned int r; // the result goes here int const mask = v >> sizeof(int) * CHAR_BIT - 1; r = (v + mask) ^ mask; Patented variation: r = (v ^ mask) - mask; What is CHAR_BIT and how use it in programming languages? c...

Question from bit twiddling site

Here is the code: unsigned int v; // word value to compute the parity of v ^= v >> 16; v ^= v >> 8; v ^= v >> 4; v &= 0xf; return (0x6996 >> v) & 1; It computes the parity of given word, v. What is the meaning of 0x6996? The number 0x6996 in binary is 110100110010110. ...

Bit Twiddling Hacks: interleave bits the obvious way

i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html) unsigned short x; // Interleave bits of x and y, so that all of the unsigned short y; // bits of x are in the even positions and y in the odd; unsigned int z = 0; // z gets the resulting Morton Number. ...

how to read I-node bitmap of a minix fs bit by bit in c

Hi, I wanted to know how can I read and check(1 or 0) the bits of the the I-node bit map of a minix fs bit by bit in c programming language. This way I want check the free and occupied I-nodes. And also if possible to set and unset the bits manually. Please help. Thank you this is the layout of the minix file system ----------------...

How to copy last X bits?

Let's say I have two integers with the following binary representations: 01101010 00110101 And now I want to copy the last 3 bits from the first integer over the second one so that it becomes 00110010 What's the easiest way to do that? (Actually, my goal is to shift the all the X+1 bits to the right one, essentially deleting the X...

Bit manipulation in C# using a mask

I need a little help with bitmap operations in C# I want to take a UInt16, isolate an arbitrary number of bits, and set them using another UInt16 value. Example: 10101010 -- Original Value 00001100 -- Mask - Isolates bits 2 and 3 Input Output 00000000 -- 10100010 00000100 -- 10100110 00001000 -- 10101010 00001100 -- 1010...

Using bitset in place of using hand written bit manipulation code?

Is there any performance loss/gain using bitset in place where hand written? How to build the following using a bitset at runtime make all the bits between 2 and 5 as zero i.e., 11110011. ...

Question about bitwise And and Shift operations

How exactly do the following lines work if pData = "abc"? pDes[1] = ( pData[0] & 0x1c ) >> 2; pDes[0] = ( pData[0] << 6 ) | ( pData[1] & 0x3f ); ...

My bit logic is too outdated. Refresher needed

It's been a while since my assembly class in college (20 years to be exact). When someone gives you a number, say 19444, and says that X is bits 15 through 8 and Y are bits 7 through 0... how do I calculate values of X and Y? I promise this is not homework, just a software guy unwisely trying to do some firmware programming. ...

Compute fast log base 2 ceiling

What is a fast way to compute the (long int) ceiling(log_2(i)), where the input and output are 64-bit integers? Solutions for signed or unsigned integers are acceptable. I suspect the best way will be a bit-twiddling method similar to those found here, but rather than attempt my own I would like to use something that is already well test...