bit-manipulation

How can I test if all bits are set or all bits are not?

房东的猫 提交于 2021-02-20 06:11:30
问题 Using bitwise operator how can I test if the n least significant bits of an integer are either all sets or all not sets. For example if n = 3 I only care about the 3 least significant bits the test should return true for 0 and 7 and false for all other values between 0 and 7. Of course I could do if x = 0 or x = 7 , but I would prefer something using bitwise operators. Bonus points if the technique can be adapted to take into accounts all the bits defined by a mask. Clarification : If I

What is the best way to shift data by X bits into and out of a file?

ぐ巨炮叔叔 提交于 2021-02-20 05:14:08
问题 I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C. void shift_4bits_left(unsigned char* array, unsigned short size) { int i; unsigned char shifted = 0x00; unsigned char overflow = (0xF0 & array[0]) >> 4; for (i = (size - 1);

What is the best way to shift data by X bits into and out of a file?

人盡茶涼 提交于 2021-02-20 05:13:59
问题 I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C. void shift_4bits_left(unsigned char* array, unsigned short size) { int i; unsigned char shifted = 0x00; unsigned char overflow = (0xF0 & array[0]) >> 4; for (i = (size - 1);

What is the best way to shift data by X bits into and out of a file?

独自空忆成欢 提交于 2021-02-20 05:13:18
问题 I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C. void shift_4bits_left(unsigned char* array, unsigned short size) { int i; unsigned char shifted = 0x00; unsigned char overflow = (0xF0 & array[0]) >> 4; for (i = (size - 1);

What is the best way to shift data by X bits into and out of a file?

限于喜欢 提交于 2021-02-20 05:12:29
问题 I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C. void shift_4bits_left(unsigned char* array, unsigned short size) { int i; unsigned char shifted = 0x00; unsigned char overflow = (0xF0 & array[0]) >> 4; for (i = (size - 1);

Extracting bits from a float in vba

我是研究僧i 提交于 2021-02-19 08:11:51
问题 How can I extract the bits from a Single variable in vba? For example, I want to extract bits 23 to 30 and place them into the lowest 8 bits of an integer. 回答1: For transferring the bit settings of the short variable to an int , the fastest solution is a 'quick and dirty' CopyMemory approach, as seen here. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Bytes As Long) Public Sub DoubleToIEEE32(ByVal dValue As Double, ByRef nI1 As

Why n bitwise and -n always return the right most bit (last bit)

依然范特西╮ 提交于 2021-02-19 05:26:36
问题 Here is the python code snippet: 1 & -1 # 1 2 & -2 # 2 3 & -3 # 1 ... It seems any n & -n always return right most (last) bit, I don't really know why. Can someone help me to understand this? 回答1: It's due to the way that negative numbers are represented in binary, which is called two's complement representation. To create the two's complement of some number n (in other words, to create the representation of -n): Invert all the bits Add 1 So in other words, when you write 1 & -1 it really

Why n bitwise and -n always return the right most bit (last bit)

南笙酒味 提交于 2021-02-19 05:26:05
问题 Here is the python code snippet: 1 & -1 # 1 2 & -2 # 2 3 & -3 # 1 ... It seems any n & -n always return right most (last) bit, I don't really know why. Can someone help me to understand this? 回答1: It's due to the way that negative numbers are represented in binary, which is called two's complement representation. To create the two's complement of some number n (in other words, to create the representation of -n): Invert all the bits Add 1 So in other words, when you write 1 & -1 it really

Check if a number is even

我只是一个虾纸丫 提交于 2021-02-17 05:56:19
问题 I'm working my way through low level bit hacks, and would like to write an assembly program for each. Here is what I have for checking if a number is even or not: is_even: # check if an integer is even. # This is the same as seeing if its a multiple of two, i.e., & 1<<n - 1 # rdi stores the number xor %eax, %eax test $0b1, %rdi setz %al ret _start: mov $5, %rdi call is_even Are there any ways to improve the above or make it more readable? Is it possible to do the is_even check with 2

Cache block tag size

自作多情 提交于 2021-02-17 05:54:06
问题 I'm writing a cache simulation program in C on linux using gcc as the compiler and I'm done for the most part. Only a few test cases go wrong (a few things out of the thousands of fed addresses that should be hitting are missing). I specify the cache properties on the command line. I suspect the error within my code has to do with the tag (if things aren't hitting then their tags aren't matching up when they should be). So my question is: Am I calculating the tag right? //setting sizes of