bit-manipulation

Verilog bit change location

非 Y 不嫁゛ 提交于 2019-12-01 14:07:12
Assuming I have a register reg [15:0] my_reg , which contains a 16-bit signed sample: How can I find the place where the first bit change is located? Meaning, that if assuming that my_reg = 16'b0001011011010111 , how can I know that the first change from 0 to 1 is at my_reg [12] ? Same for numbers starting with 1 ,negative numbers, e.g. my_reg = 16'b1111011011010111 would be interested in the position of the first appearing 0 (which is 11 in this case). The ultimate goal (to add a little bit of context) is to implement a digital FPGA built-in automatic gain control (AGC). Guy Same technique as

Pack numbers into a bitset (python,bitwise operations)

无人久伴 提交于 2019-12-01 14:00:48
问题 The PIC microcontroller has a dead simple instruction set format. Each instruction is exactly 14 bits long, composed of a variety of numbers at differing bit lengths. I am trying to build a function that can take all these inputs and build a number that represents that instruction. This is what I have been trying to get working: def fileRegOp(opcode, d, f): out = opcode << 13 out = out | d << 7 out = out | f return out print "FIN:", bin(fileRegOp(1,True,15)) It outputs FIN: 0b10000010001111

Bit Twiddling in C - Counting Bits

房东的猫 提交于 2019-12-01 13:22:04
I want to count the bits that are set in an extremely large bit-vector (i.e. 100,000 bits). What I am currently doing is using a pointer to char (i.e. char *cPtr) to point to the beginning of the array of bits. I then: 1. look at each element of the array (i.e. cPtr[x]), 2. convert it to an integer (i.e. (int) cPtr[x]) 3. use a 256 element look-up table to see how many bits are set in the given byte (i.e. cPtr[x]). It occurs to me that if I use a short int pointer instead (i.e. short int * sPtr), then I will only need half as many look-ups, but with a 65534 element look-up table, which will

XOR bitset when 2D bitset is stored as 1D

老子叫甜甜 提交于 2019-12-01 13:13:55
To answer How to store binary data when you only care about speed? , I am trying to write some to do comparisons, so I want to use std::bitset . However, for fair comparison, I would like a 1D std::bitset to emulate a 2D. So instead of having: bitset<3> b1(string("010")); bitset<3> b2(string("111")); I would like to use: bitset<2 * 3> b1(string("010111")); to optimize data locality. However, now I am having problem with How should I store and compute Hamming distance between binary codes? , as seen in my minimal example: #include <vector> #include <iostream> #include <random> #include <cmath>

I need bit manipulation guide / reference material for c# [duplicate]

a 夏天 提交于 2019-12-01 12:34:44
问题 This question already has answers here : Closed 10 years ago . Possible Duplicate: Most common C# bitwise operations I am looking for bit manipulation reference material for c#. And, OR, XOR, left shift , right shift. setting a bit All operations with bit. 回答1: This was just posted today and should be helpful. It covers a variety of bit operations, and the operations should be translatable directly to C#. 回答2: These are all the standard C-style operators. http://www.space.unibe.ch/comp_doc/c

Breaking up a LPARAM variable & looking at groups of bits

泄露秘密 提交于 2019-12-01 12:31:04
问题 I know that a LPARAM variable has certain bits set(inside it) that identify information such as long key presses & etc. when I receive a WM_KEYDOWN event. So I am trying to break up a LPARAM variable & look at groups of individual bit values & groups of bits & that value(for eg looking at the 16th to the 24th bit & the value from that). My Problem: I dont know how to look at individual bits & groups of bits? How do I break up the LPARAM variable & look at bit values (printing it out in binary

Find bit position without using Log()

随声附和 提交于 2019-12-01 12:30:29
I have an integer input that is power of 2 (1, 2, 4, 8 etc). I want the function to return bit position without using log(). For example, for inputs above will return {0, 1, 2, 3} respectively This for C#. Plus if this can be done in SQL. Thanks! The fastest code I found to do this is from the Bit Twiddling Hacks site. Specifically, the lookup based on the DeBruijn sequence. See http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn I tested a naive method, a switch-based method, and two of the Bit Twiddling Hacks methods: the DeBruijn sequence, and the other that says, "if you

using bitwise OR in javascript to convert to integer

被刻印的时光 ゝ 提交于 2019-12-01 12:28:14
we can do the following to convert: var a = "129.13"|0, // becomes 129 var b = 11.12|0; // becomes 11 var c = "112"|0; // becomes 112 This seem to work but not sure if this is a standard JS feature. Does any one have any idea if this is safe to use for converting strings and decimals to integers ? Yes, it is standard behavior. Bitwise operators only operate on integers, so they convert whatever number they're give to signed 32 bit integer. This means that the max range is that of signed 32 bit integer minus 1, which is 2147483647 . (Math.pow(2, 32) / 2 - 1)|0; // 2147483647 (Math.pow(2, 32) /

What does '<<' mean in C?

ぃ、小莉子 提交于 2019-12-01 12:21:05
what does this mean? #define WS_RECURSIVE (1 << 0) I understand that it will define WS_Recursive (1 << 0) but what does << mean? Thanks! << is the left shift operator . It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 . It is commonly used to create flags , numbers that can be combined together with | (bit or) and various operations can be applied to them, such as testing whether a flag is set, setting a flag, removing a flag, etc. The reason that they can be combined together without interfering with each other is that each one is a power of two , and that

Implement greater equal sign in C using only bitwise operations

半城伤御伤魂 提交于 2019-12-01 11:41:46
问题 I know that many basic operations like addition or division can also be implemented in C using only bitwise operators. How can I do the same with the greater than or equal sign (>=)? if (x >= 0) { ... } 回答1: Simplest solution I can come up with: #include <limits.h> if ((x & INT_MAX) == x) // if (x >= 0) ... If you don't like the == then use XOR to do the equals test: #include <limits.h> if ((x & INT_MAX) ^ x) // if (x < 0) ... else // else x >= 0 ... 回答2: If you only want if (x >= 0) then