bit-manipulation

C - Get a bit from a byte [duplicate]

落爺英雄遲暮 提交于 2019-12-03 08:56:31
This question already has answers here : How do I get bit-by-bit data from an integer value in C? (8 answers) Possible Duplicate: how to get bit by bit data from a integer value in c? I have a 8-bit byte and I want to get a bit from this byte, like getByte(0b01001100, 3) = 1 Firstoff, 0b prefix is not C but a GCC extension of C. To get the value of the bit 3 of an uint8_t a , you can use this expression: ((a >> 3) & 0x01) which would be evaluated to 1 if bit 3 is set and 0 if bit 3 is not set. First of all C 0b01... doesn't have binary constants, try using hexadecimal ones. Second: uint8_t

Rotating a bitmap 90 degrees

一个人想着一个人 提交于 2019-12-03 08:56:26
问题 I have a one 64-bit integer , which I need to rotate 90 degrees in 8 x 8 area (preferably with straight bit-manipulation). I cannot figure out any handy algorithm for that. For instance, this: // 0xD000000000000000 = 1101000000000000000000000000000000000000000000000000000000000000 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 after rotation becomes this: // 0x101000100000000 =

Python equivalent of C code from Bit Twiddling Hacks?

﹥>﹥吖頭↗ 提交于 2019-12-03 08:51:36
I have a bit counting method that I am trying to make as fast as possible. I want to try the algorithm below from Bit Twiddling Hacks , but I don't know C. What is 'type T' and what is the python equivalent of (T)~(T)0/3? A generalization of the best bit counting method to integers of bit-widths upto 128 (parameterized by type T) is this: v = v - ((v >> 1) & (T)~(T)0/3); // temp v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp c = (T)(v * ((T)~(T)0/255)) >> (sizeof(v) - 1) * CHAR_BIT; // count T is a integer type, which I'm assuming is

Conditional Using Bitwise Operators

寵の児 提交于 2019-12-03 08:36:15
How is the conditional operator represented using bitwise operators? It is a homework question where I have to implement the conditional operator using only bitwise operations. It would be simple if if statements were allowed, however it has to be strictly bitwise operators. Only the operators ! , ~ , & , ^ , | , + , >> , and << can be used. No if statements or loops can be used. The function takes three ints and works just like the normal conditional operator. The first argument is evaluated as either zero or non-zero. If the first argument is zero then the second argument is returned. If the

How to judge if there is even 1s in a number's binary representation using C? [duplicate]

假装没事ソ 提交于 2019-12-03 08:25:30
This question already has answers here : Computing the Parity (2 answers) There are already questions about counting how many 1 s are there in a number, but this question is about judging whether there is even or odd number of 1s. Any loop or conditional (including switch) statements are not allowed. Also, division, multiplication or modulus operators should be avoided. To be more specific, we may assume that it's a 32-bits unsigned integer. Actually I've got an implementation already, but I cannot work out the reason why it works. Any proof of its correctness or any new idea would be very

Check division by 3 with binary operations?

风格不统一 提交于 2019-12-03 08:12:45
I've read this interesting answer about " Checking if a number is divisible by 3 " Although the answer is in Java , it seems to work with other languages also. Obviously we can do : boolean canBeDevidedBy3 = (i % 3) == 0; But the interesting part was this other calculation : boolean canBeDevidedBy3 = ((int) (i * 0x55555556L >> 30) & 3) == 0; For simplicity : 0x55555556L = "1010101010101010101010101010110" Nb There's also another method to check it : One can determine if an integer is divisible by 3 by counting the 1 bits at odd bit positions, multiply this number by 2, add the number of 1-bits

How to reverse bits of a byte?

三世轮回 提交于 2019-12-03 07:59:56
For example, in PHP, how would I reverse the bits of the byte 11011111 to 11111011 ? The straight forward approach is to perform 8 masks, 8 rotates, and 7 additions: $blah = $blah & 128 >> 7 + $blah & 64 >> 5 + $blah & 32 >> 3 + $blah & 16 >> 1 + $blah & 8 << 1 + $blah & 4 << 3 + $blah & 2 << 5 + $blah & 1 << 7; If you have already the bits in the form of a string, use strrev . If not, convert first the byte to its binary representation by using decbin , then reverse using strrev, then go back to byte (if necessary) by using bindec . Check the section on reversing bit sequences in Bit

Verilog signed vs unsigned samples and first

谁说胖子不能爱 提交于 2019-12-03 07:44:58
Assuming I have a register reg [15:0] my_reg , which contains a 16-bit signed sample: How do I convert the sample from signed to unsigned? I have read this Wikipedia article , and am aware of the 2-bit complement for signed numbers, but how do I perform this conversion in Verilog efficiently? (I don't know if my_reg is positive or negatve, and it changes in every clock cycle = I receive a new sample on every positive clock edge). The ultimate goal (to add a little bit of context) is to implement a digital FPGA built-in automatic gain control (AGC). EDIT: as suggested I have split the two

Is a bit field any more efficient (computationally) than masking bits and extracting the data by hand?

人盡茶涼 提交于 2019-12-03 07:00:47
I have a numerous small pieces of data that I want to be able to shove into one larger data type. Let's say that, hypothetically, this is a date and time. The obvious method is via a bit field like this. struct dt { unsigned long minute :6; unsigned long hour :5; unsigned long day :5; unsigned long month :4; unsigned long year :12; }stamp; Now let's pretend that this thing is ordered so that things declared first are at bits of higher significance than things declared later so if I represent the bits by the first letter of the variable it would look like: mmmmmm|hhhhh|ddddd|mmmm|yyyyyyyyyyyy

Finding Bit Positions in an unsigned 32-bit integer

谁都会走 提交于 2019-12-03 06:59:08
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 numbered from 1 (low-order) to 32 (high-order). All undefined flag bits are reserved and must be set to 0. I have a Table that shows the meanings of the flags, with meaning for the numbers 1-10. I am hoping that someone can try and explain to me what this all means