bit-manipulation

Why do a lot of languages lack a logical XOR operator?

Off the top of my head, I cannot think of a single language I've used that had a logical exclusive or operator, but all have logical and bitwise and and or operators. Looking around, the only reason to this that I could find was that exclusive or cannot be short circuited, so a logical version would be useless, which I really can't see ...

Handling arbitrary bit length data in Delphi?

I am working on a display/control utility to replace an ancient dedicated hardware controller for a piece of industrial machinary. The controller itself is beyond repair (someone replaced the 1 amp fuse with a 13 amp one "because it kept blowing"). The hardware interface is through a standard RS232 port. The data format is dedicated: N...

shifting right / what I am doing wrong?

Does not work as expected becuase it does not set the MSB bit correct. I am using metrowerks compiler. //shifting right 5 characters char * buffer; buffer=global_buffer; for(i=0;i<5;i++) //shift right for 1; { buffer[17-i]=(buffer[17-i]>>1)|(buffer[17-i-1]<<7); } EDIT input buffer (just before for loop) 0x00,0x00,0x00,0x00,0x00,0x...

How do you clear the read-only flag on a file in .NET?

How do you clear the read-only flag on a file in .NET and leave the rest intact? ...

Converting double to float without relying on the FPU rounding mode

Does anyone have handy the snippets of code to convert an IEEE 754 double to the immediately inferior (resp. superior) float, without changing or assuming anything about the FPU's current rounding mode? Note: this constraint probably implies not using the FPU at all. I expect the simplest way to do it in these conditions is to read the...

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

How do I determine which bit is set in an unsigned int64

I have a variable vBit which is an unsigned int64. I know there is exactly one bit set, and I need to figure out which one it is. Currently I do it like this (in Delphi): vPos := -1; repeat vBit := vBit shr 1; inc(vPos); until vBit = 0; Is there a faster way? All bit positions are equally likely, so on average the algorithm needs ...

bit operations in c

how do you reverse and rotate hex numbers and return the number in C with bitwise operators? for example: 0xabcd -> 0xdcba 0xabcd -> 0xdabc ...

Multiply without * operator

I was just going through some basic stuff as I am learning C. I came upon a question to multiply a number by 7 without using * operator. Basically its like this (x<<3)-x; Now I know about basic bit manipulation operations, But I cant get how do you multiply a number by any other odd number number without using * operator?? Is th...

Generate a random binary number with a variable proportion of '1' bits

I need a function to generate random integers. (assume Java long type for now, but this will be extended to BigInteger or BitSet later.) The tricky part is there is a parameter P that specifies the (independent) probability of any bit in the result being 1. If P = 0.5 then we can just use the standard random number generator. Some oth...

Very Compact Bitarray in Java

I'm looking for a very compact way of storing a dense variable length bitarray in Java. Right now, I'm using BitSet, but it seems to use on average 1.5*n bits of storage space for a bit vector of size n. Typically, this isn't a problem, but in this case the bitarrays being stored are a pretty significant part the memory footprint of the ...

implement eq, lt gt in assembly without jumps

Is it possible to write logic using only AND, OR NOT operators to compare 2 operands and return true/false (-1, 0) without the use of jumps. If so can you please give me some hints as it looks impossible to me I don't know if I am explaining this very well but I am trying to implement eq, lt and gt in the assembly language of the book "T...

How do I represent and work with n-bit vectors in Python?

In an assignment I am currently working on we need to work with bit vectors, but I am very unsure of how to do this in Python. They should be able to be from 4 bits to 20 bits. I have never worked with bit vector before, but I guess that one would one create arrays of unsigned bytes that you manipulated using the usual AND/OR/XOR operati...

Any smarter way to extract from array of bits?

I have areas of memory that could be considered "array of bits". They are equivalent to unsigned char arr[256]; But it would be better thought of as bit arr[2048]; I'm accessing separate bits from it with #define GETBIT(x,in) ((in)[ ((x)/8) ] & 1<<(7-((x)%8))) but I do it a lot in many places of the code, often in performance-...

Reading characters on a bit level

I would like to be able to enter a character from the keyboard and display the binary code for said key in the format 00000001 for example. Furthermore i would also like to read the bits in a way that allows me to output if they are true or false. e.g. 01010101 = false,true,false,true,false,true,false,true I would post an idea of ho...

Assistance in basic bit manipulation

This is a follow on question from my previously answered question here: http://stackoverflow.com/questions/2185719/reading-characters-on-a-bit-level The problem that I seem to be having is understanding the basic concepts of bit manipulation in C. I understand what needs to be done but I am having trouble actually developing a solution...

How to get lg2 of a number that is 2^k

What is the best solution for getting the base 2 logarithm of a number that I know is a power of two (2^k). (Of course I know only the value 2^k not k itself.) One way I thought of doing is by subtracting 1 and then doing a bitcount: lg2(n) = bitcount( n - 1 ) = k, iff k is an integer 0b10000 - 1 = 0b01111, bitcount(0b01111) = 4 But ...

Extracting the value represented by a particular set of bits in a number

How to extract the value represented by a particular set of bits in a given number i.e. if bits 11,12 & 13 are 1,1,0 then the value should be 6. What is the most efficient way of doing the same? Also, it should be generic. I should be able to give start and end bit positions and should be able to extract the value represented by the bit...

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

Package for fast determination of similarity between two bit sequences

I need to compare a query bit sequence with a database of up to a million bit sequences. All bit sequences are 100 bits long. I need the lookup to be as fast as possible. Are there any packages out there for fast determination of the similarity between two bit sequences? --Edit-- The bit sequences are position sensitive. I have seen a p...