bitwise

how do i define 24 bit array in c++?

how do i define 24 bit array in c++? (variable declaration) thank you ...

Weird output for bitwise NOT

I am trying to take one's complement of 0 to get 1 but i get 4294967295. Here is what i have done: unsigned int x = 0; unsigned int y= ~x; cout << y; My output is 4294967295 but I expect 1, why is this so?. By the way, i am doing this in C++. ...

JAVA bitwise operation help.

Hello, Lets say I have a number 345, I want to have so that I end up with 0345. I.e. int i = 0345; How can I take an existing number and shift it along or append a 0. Thanks ...

How do you express (Keys.Control | Keys.M) in F#?

In C#, you can express characters for the KeyPress event in the form Keys.Control | Keys.M. In F#, Keys.Control ||| Keys.M doesn't work. What does? Edit: Interesting indeed. Using System.Windows.Forms.Keys.Control ||| System.Windows.Forms.Keys.M as per Johannes Rössel's suggestion below in the F# interactive window works exactly as he s...

What is the correct way to check if bit field is turn on in php

What is the correct way to check if bit field is turn on - (in php) ? I want to check a bit field that come from db(mysql) if is turn on or not. is this is the correct way ? if($bit & 1) Are there other ways ? I see somebody code that using ord() function , it is correct ? like if(ord($bit) == 1) ...

How to convert byte string into Number?

Hi all, I have a string encoded in Little Endian format (least-significant-byte first), and I want to decode it into a Number. For the encoder, the first bit of the last byte reflects whether the number is positive or negitive. Does anyone know how to do this? Here's my decoder: decode:function(str,type){ var num=0; va...

Choosing values for constants

One thing I've never really understood is why in many libraries, constants are defined like this: public static final int DM_FILL_BACKGROUND = 0x2; public static final int DM_FILL_PREVIOUS = 0x3; public static final int TRANSPARENCY_MASK = 1 << 1; public static final int TRANSPARENCY_PIXEL = 1 << 2; What's up with the 0x and << stuff?...

bitwise operations

// PWM frequency: // 0 - 48 kHz // 1 - 12 kHz // 2 - 3 kHz enum { MOTOR_FREQUENCY = 1 }; // Configure Timer 2 w. 250x period. T2CON = 1 << 2 | MOTOR_FREQUENCY /* << 0 */; Have i understood this right? 11111111 Arithmetic left-shift-by-two of 0 or 1 or 2 Means: T2CON = 1 << 2 | 0 = 1111 1100 T2CON = 1 << 2 | 1 = 1111 1000 ...

Java: How to replace last 16 bits of a long with a short

I have a long and a short I want the bits from the short to overwrite the low order 16 bits of the long. Ex (broken into 16bit chunks for readability): > long = 0xffff 0xffff 0xffff 0xffff > short= 0x1234 > > output = (long)0xffff 0xffff 0xffff 0x1234 ...

Swapping bits at a given point between two bytes

Let's say I have these two numbers: x = 0xB7 y = 0xD9 Their binary representations are: x = 1011 0111 y = 1101 1001 Now I want to crossover (GA) at a given point, say from position 4 onwards. The expected result should be: x = 1011 1001 y = 1101 0111 Bitwise, how can I achieve this? ...

PHP funtion flags, how?

I'm attempting to create a function with flags as its arguments but the output is always different with what's expected : define("FLAG_A", 1); define("FLAG_B", 4); define("FLAG_C", 7); function test_flags($flags) { if($flags & FLAG_A) echo "A"; if($flags & FLAG_B) echo "B"; if($flags & FLAG_C) echo "C"; } test_flags(...

c bitwise negation conversion question

the following code: signed char sc = ~0xFC; unsigned char uc = ~0xFC; when compiled gives me the following warnings: integer conversion resulted in truncation integer conversion resulted in truncation why do i get these warnings how do i write my code, so that i dont get these warnings (without using #pragmas) thanx, i'm using ...

How to do bitwise AND in javascript on variables that are longer than 32 bit?

I have 2 numbers in javascript that I want to bit and. They both are 33bit long in C#: ((4294967296 & 4294967296 )==0) is false but in javascript: ((4294967296 & 4294967296 )==0) is true 4294967296 is ((long)1) << 32 As I understand it, it is due to the fact that javascript converts values to int32 when performing bit wise ope...

best book for bit/binary manipulation

I've looked at some interview questions and they required heavy bit manipulations. Could anyone please recommend a good book that would allow me to polish my skills in bit manipulation ? (from beginner to intermediate/advanced) with good exercises to pratice ? ...

iPhone Operators

Okay so I have defined my DSNavigationManager class and it has a property called DSNavigationManagerStyle managerStyle: typedef enum { DSNavigationManagerStyleNone = 0, DSNavigationManagerStyleDefaultNavigationBar = 1 << 0, DSNavigationManagerStyleDefaultToolBar = 1 << 1, DSNa...

Valid use of bitwise operators for CreateParams, undesired behavior?

I am writing a wrapper for the ProgressBar control (not really a true wrapper, but implementing Vista features properly). And here is my code: /// <summary> /// Encapsulates the information needed when creating a control /// </summary> protected override CreateParams CreateParams { [SecurityPermission(SecurityAct...

Are there any good reasons to use bit shifting except for quick math?

I understand bitwise operations and how they might be useful for different purposes, e.g. permissions. However, I don't seem to understand what use the bit shift operators are. I understand how they work, but I can't think of any scenarios where I might want to use them unless I want to do some really quick multiplication or division. Ar...

Logical, arithmetical bitwise shifts

Seeking to clarify something. It is my understanding that with regard to arithmetical, logical bitwise shifts: << work the same for both >> shifts differ in which logical shift will always pad byte with 0, whereas arithmetical shift will pad it with the sign bit. How can i differentiate this using C? From what i understand, actual...

negation using bitwise operators, C

Suppose you have 2 numbers: int x = 1; int y = 2; Without worrying about specific difference between them, i'd like to know whether x is larger then y using only bitwise operators. I can check to see if number is positive or negative by using !(x >> 31), but before i get there, i need to implement negation using bitwise operators. ...

bitwise multiplication problem

I am working through a problem which i was able to solve, all but for the last piece - i am not sure how can one do multiplication using bitwise operators: 0*8 = 0 1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 Can you please recommend an approach to solve this? ...