bitwise

Is there a better way to do a 4 bits circular shifting?

is this the optimal way of doing a 4 bits circular shifting? n << 1 ^ 0x10 | n >> 3 I was only testing with number that was actually "working"! ...

C quick calculation of next multiple of 4?

what's a fast way to roundup a unsigned int to a multiple of 4? a multiple of 4 has the two least significant bits 0, right? so i could mask them out and then do a switch statement, adding either 1,2 or 3 to the given uint. that's not a very elegant solution.. there's also the arithmetic roundup: myint==0?0:((myint+3)/4)*4 probably t...

Parsing a bit field parameter, how to "discard" bits in an unsigned long?

Hey all, First of all, I want to know if this is possible: let's say I have an unsigned long which contains some abritrary unsigned shorts, which may or may not be in the number. For example: unsigned short int id1 = 3456, id2 = 30998; unsigned long long bitfld = id1|id2; Can the other 2 fields be assumed as 0? An...

Help with bitwise expression

I want a one line code to check whether a given integer is of form 2i - 2j or NOT. (using bitwise operators) I tried to do it but I don't seem to be getting the answer. Can anyone please help me with this? It's kinda urgent... :-| Thanks. ...

Bitwise-or seems to have an implicit cast to long. Can this be avoided?

I'm a Java programmer trying to migrate to C#, and this gotcha has me slightly stumped: int a = 1; a = 0x08000000 | a; a = 0x80000000 | a; The first line compiles just fine. The second does not. It seems to recognise that there is a constant with a sign bit, and for some reason it decides to cast the result to a long, resulting in th...

k&r exercise confusion with bit-operations

The exercise is: 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. My attempt at a solution is: #include <stdio.h> unsigned setbits(unsigned, int, int, unsigned); int main(void) { printf("%u\n", setbits(256, 4, 2, 255)); ...

Check to see if a variable doesn't have Bitwise & X?

I have been using a bitwise comparison to check if entities and maptiles have flags in a roguelike game, but I've run into a problem - I need to check in an if() if a tile/ent doesn't have a flag, but I can't figure out how to do it without using an empty if() {} and else { condition; }, an example being: if(Tile->Flags & TILE_INVIEW) {...

C/C++ bitfields versus bitwise operators to single out bits, which is faster, better, more portable?

I need to pack some bits in a byte in this fashion: struct { char bit0: 1; char bit1: 1; } a; if( a.bit1 ) /* etc */ Or: if( a & 0x2 ) /* etc */ From the source code clarity it's pretty obvious to me that bitfields are neater. But which option is faster? I know the speed difference won't be too much if any, but as I can use any of th...

bitwise operations in PHP?

I understand that bitwise operations are necessary for much low-level programming, such as writing device drivers, low-level graphics, communications protocol packet assembly and decoding. I am doing PHP for several years now, I have seen bitwise operations very rarely in PHP projects. Can you give me examples of usage? ...

Splitting an rgb uint into its seperate r g b components

I have an rgb colour stored as an uint. I can create this from the rgb values using the bitwise left and bitwise or opperator in an expression like this: colour = r<<16 | g<<8 | b; I want to do the opposite. I have the final number and I want the r, g and b values. Does anyone know how to do this? ...

Convert an unsigned 16 bit int to a signed 16 bit int in C#

I'm writing a datalog parser for a robot controller, and what's coming in from the data log is a number in the range of 0 - 65535 (which is a 16 bit unsigned integer if I'm not mistaken). I'm trying to convert that to a signed 16 bit integer to display to the user (since that was the actual datatype before the logger changed it). Can so...

Getting 32 bit words out of 64-bit values in C/C++ and not worrying about endianness...

It's my understanding that in C/C++ bitwise operators are supposed to be endian independent and behave the way you expect. I want to make sure that I'm truly getting the most significant and least significant words out of a 64-bit value and not worry about endianness of the machine. Here's an example: uint64_t temp; uint32_t msw, lsw;...

Bitwise equality

I need to perform a bitwise equality between two bytes. That means that for instance if I have two bytes: 00011011 and 00011110 the result is 11111010 The only fast way I see is to use the following statement byte a, b;//set input bytes byte c = ~(a^b);//output bytes But I wonder if there is a faster solution for this. After these equ...

How does this work? Weird Towers of Hanoi Solution

I was lost on the internet when I discovered this unusual, iterative solution to the towers of Hanoi: for (int x=1; x < (1 << nDisks); x++) { FromPole = (x&x-1)%3; ToPole = ((x|x-1)+1)%3; moveDisk(FromPole,ToPole); } This post also has similar Delphi code in one of the answers. However, for the life of me, ...

Bitwise operation on floating point numbers (for graphics)?

Possible Duplicate: how to perform bitwise operation on floating point numbers Hello, everyone! Background: I know that it is possible to apply bitwise operation on graphics (for example XOR). I also know, that in graphic programs, graphic data is often stored in floating point data types (to be able for example to "multiply"...

Zig Zag Decoding

In the google protocol buffers encoding overview, they introduce something called "Zig Zag Encoding", this takes signed numbers, which have a small magnitude, and creates a series of unsigned numbers which have a small magnitude. For example Encoded => Plain 0 => 0 1 => -1 2 => 1 3 => -2 4 => 2 5 => -3 6 => 3 And so on. The encoding ...

Bitwise operations on Java Enumerations; RE: Chess E.G.

If I were keeping an array in C representing a chess board, I might fill it with enumed items that roughly look like: enum CHESSPIECE { none = 0, pawn, knight, bishop, rook, queen, king, type_mask = 7, white = 8, white_pawn, white_knight, white_bishop, white_rook, white_queen, white_king, black = 16, black_pawn, black_kig...

function explanation in php

Hi i have one function but i am not getting what is it doing. Below is my function // My function gets two parameters lat and long public function generate_peano1($lat, $lon) { $lat = (($lat + 90.0)/180.0 * 32767) + 16384; $lon = ($lon + 180.0)/360.0 * 65535; $lat_16 = $lat&0x0000FFFF; // Not getting what is here. $...

Persisting a set of Days of the Week

I'm looking for an efficient way to persist a set of Days of the Week. In other words, a user will select any number of days from a list of weekdays. The specific date doesn't matter, just the day of the week. I need to store this set of days in a hibernate entity. I could store one of these: Set<DayOfWeek> Set<Integer> But I'm thi...

Set Range of Bits in a ushort

Lets say I have a ushort value that I would like to set bits 1 to 4 inclusive (assuming 0 is the LSB and 15 is the MSB). In C++ you could define a struct that mapped out specific bits: struct KibblesNBits { unsigned short int TheStart: 1; unsigned short int TheMeat: 4; unsigned short int TheRest: 11; } Then you could assign...