bit-manipulation

Cast Integer to Float using Bit Manipulation breaks on some integers in C

倖福魔咒の 提交于 2019-12-11 08:00:59
问题 Working on a class assignment, I'm trying to cast an integer to a float only using bit manipulations (limited to any integer/unsigned operations incl. ||, &&. also if, while). My code is working for most values, but some values are not generating the results I'm looking for. For example, if x is 0x807fffff, I get 0xceff0001, but the correct result should be 0xceff0000. I think I'm missing something with my mantissa and rounding, but can't quite pin it down. I've looked at some other threads

IEEE 754 Bit manipulation Rounding Error

不想你离开。 提交于 2019-12-11 07:35:47
问题 Without using casts or functionality of libraries, I must cast an integer to a float with bit manipulation. Below is the code I am currently working on. It is based off of code that I found in Cast Integer to Float using Bit Manipulation breaks on some integers in C. The problem that I have ran into involves the rounding standards in IEEE 754. More specifically my code rounds towards 0, but it should round towards even numbers. What changes do I need to make? unsigned inttofloat(int x) { int

difference between similar bitwise operators

Deadly 提交于 2019-12-11 07:24:50
问题 in refereing to bit-wise operators, what is the difference between ! and ~ ? I feel like they both flip the bits, but 1 maybe adds a 1 to the final answer? ~0xC4 compared to !0xC4 Thanks! 回答1: ! is not a bitwise operator, it's a boolean operator. The boolean operators operate on truth values, which are generally int . Any non-zero value is true, while 0 is false. The result is always 1 for true, 0 for false. ! is boolean not && is boolean and || is boolean or These are the ones used in e.g.

How to “rotate” the binary data (in horizontal direction) if its size is greater than 32 bits?

佐手、 提交于 2019-12-11 07:18:08
问题 I have the following TypedArray (note that the size of this data is 80 bits): var arr = new Uint8Array([10, 110, 206, 117, 200, 35, 99, 2, 98, 125]); and I want to rotate it by N bits (where N is any integer from 0 to 79). For example, if N=50, I will represent it like this: 00001010 01101110 11001110 01110101 11001000 00100011 01100011 00000010 01100010 01111101 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ so the result should be 01101100 01100000 01001100 01001111 10100001 01001101

Explanation for computing the nearest power of two greater than the argument using bit twiddling

我怕爱的太早我们不能终老 提交于 2019-12-11 07:07:52
问题 This is a well-known function to compute the next nearest power of two for positive arguments. However I don't have much experience in bit twiddling to grok the logic/theory behind it. Would you kindly explain why and how that works? Particularly, the choice of 1,2,4,8,16 for shifting and if the range of the argument was greater what would be used, for example, for a long? Why logical shift instead of arithmetic and finally, what does ORing shifted arg accomplish? static int

Rotate left on a 64-bit word byte array in JavaCard

老子叫甜甜 提交于 2019-12-11 06:46:14
问题 I am trying to perform a rotate left (ROTL) operation on an arbitrary amount of rotations on a 64-bit word currently represented as a byte array of 8 bytes in a JavaCard smart card. The ugly way around is to hard-code all 64 possible permutations of ROTL on a 64-bit word represented as an 8 byte array but that will simply bloat the entire codebase up. How do I make it leaner so that I can on-the-fly do any arbitrary amount of ROTL operations on demand on a 64-bit word (in byte array) with the

Integer overflow with subtraction

僤鯓⒐⒋嵵緔 提交于 2019-12-11 06:23:53
问题 Okay so I need to implement a method that returns zero or one depending on if x-y results in an overflow. Example: subTract(0x80000000,0x80000000) = 1, subTract(0x80000000,0x70000000) = 0, I'm NOT looking for an implementation of this method. I don't understand which one supposedly results in an overflow, and that making it nearly impossible to start. Why does one of these cause an overflow? What defines an overflow with subtraction. assume the system uses 2's complement and a 32-bit

a macro to count bits that are on (set)

北城余情 提交于 2019-12-11 05:48:10
问题 My task is to write a macro that checks how many elements in an array of INTs has excatly 5 bits that are on. I know a macro is a very risky way of doing it, but that is the question that appears in some exams. This is my code: #include <stdio.h> #define RESULT 5 #define SIZE 8 #define BITW(arr, length, counter)\ int mask=0b00000001, bits=0, i=0, j=0;\ for (i=0; i<length; i++){\ for (j=0; j<sizeof(arr[i])*SIZE; j++){\ if(mask&arr[i]>>j)\ bits++;\ }\ if (bits==RESULT)\ counter++;\ } int main

bitwise anding unsigned long with 0xffffffff [closed]

微笑、不失礼 提交于 2019-12-11 05:32:38
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . Below is some code from switch statement. getvalue() returns an unsigned long . Could somebody explain why value is bitwise anded with 0xffffffff . The mcu is 32 bit. #define WriteMemory(A,V) *(volatile unsigned long*)(A)=(V) static unsigned value; case 'b': value = getvalue();

Storing DaysOfWeek as single integer in sqlite database using enum/bitwise - Java

故事扮演 提交于 2019-12-11 05:32:01
问题 Here's what I'm trying to do. I have a field that encompasses multiple days per week. (E.g. it can store MONDAY or MONDAY | TUESDAY, or WEDNESDAY | FRIDAY) - basically any combination. To do this I used an enum with powers of 2, like this: public enum DayOfWeek { Monday(1), Tuesday(2), Wednesday(4), Thursday(8), Friday(16), Saturday(32), Sunday(64); private final int index; DayOfWeek(int index) { this.index = index; } public int value() { return index; } } Then I store an integer in the