bit-manipulation

Integer Byte Swapping in C++

折月煮酒 提交于 2019-12-09 12:28:39
问题 I'm working on a homework assignment for my C++ class. The question I am working on reads as follows: Write a function that takes an unsigned short int (2 bytes) and swaps the bytes. For example, if the x = 258 ( 00000001 00000010 ) after the swap, x will be 513 ( 00000010 00000001 ). Here is my code so far: #include <iostream> using namespace std; unsigned short int ByteSwap(unsigned short int *x); int main() { unsigned short int x = 258; ByteSwap(&x); cout << endl << x << endl; system(

OpenGL stencil buffer OR operation?

余生颓废 提交于 2019-12-09 11:59:25
问题 I'm not sure if this is possible to do, but it's worth a shot. I'm using the stencil buffer to reduce overdraw for light volumes in a deferred renderer using this algorithm (when the camera is outside the volume): Using a cheap shader, draw back faces with depth testing set to LEQUAL, marking them in the stencil buffer. Using the expensive lighting shader, draw front faces with depth testing set to GEQUAL. This will cause only pixels within the light volume to be shaded. The problem with this

Writing files in bit form to a file in C

心不动则不痛 提交于 2019-12-09 10:07:04
问题 I am implementing the huffman algorithm in C. I have got the basic functionality down up to the point where the binary codewords are obtained. so for example, abcd will be 100011000 or something similar. now the question is how do you write this code in binary form in the compressed file. I mean if I write it normally each 1 and 0 will be one character so there is no compression. I need to write those 1s and 0s in their bit form. is that possible in C. if so how? 回答1: Collect bits until you

How do you implement XOR using +-*/?

。_饼干妹妹 提交于 2019-12-09 09:18:36
问题 How can the XOR operation (on two 32 bit ints) be implemented using only basic arithmetic operations? Do you have to do it bitwise after dividing by each power of 2 in turn, or is there a shortcut? I don't care about execution speed so much as about the simplest, shortest code. Edit: This is not homework, but a riddle posed on a hacker.org. The point is to implement XOR on a stack-based virtual machine with very limited operations (similar to the brainfuck language and yes - no shift or mod).

Invert 1 bit in C#

自作多情 提交于 2019-12-09 08:25:51
问题 I have 1 bit in a byte (always in the lowest order position) that I'd like to invert. ie given 00000001 I'd like to get 00000000 and with 00000000 I'd like 00000001. I solved it like this: bit > 0 ? 0 : 1; I'm curious to see how else it could be done. 回答1: How about: bit ^= 1; This simply XOR's the first bit with 1, which toggles it. If you want to flip bit #N, counting from 0 on the right towards 7 on the left (for a byte), you can use this expression: bit ^= (1 << N); This won't disturb any

bitwise & doesn't work with bytes in kotlin

丶灬走出姿态 提交于 2019-12-09 07:54:04
问题 I'm trying to write kotlin code like: for (byte b : hash) stringBuilder.append(String.format("%02x", b&0xff)); but I have nothing to do with the "&". I'm trying to use "b and 0xff" but it doesn't work. The bitwise "and" seems to work on Int, not byte. java.lang.String.format("%02x", (b and 0xff)) it's ok to use 1 and 0xff 回答1: Kolin provides bitwise operator-like infix functions available for Int and Long only. So it's necessary to convert bytes to ints to perform bitwise ops: val b : Byte =

Using Powershell's bitwise operators

一世执手 提交于 2019-12-09 07:50:23
问题 I'm looking for example of how I would solve the scenario below: Imagine my printer has the following property for "Status" 0 -Offline 2 -Paper Tray Empty 4 -Toner Exhausted 8 -Paper Jam When I query status it returns a value of 12. I can obviously see that this means the printer has the Toner Exhausted and a Paper Jam, but how would I work this out with Powershell? Thanks 回答1: The boolean bitwise and operator in Powershell is -band . Assume you define your values and descriptions in a

How to find TMax without using shifts

强颜欢笑 提交于 2019-12-09 07:32:27
Using ONLY ! ~ & ^ | + How can I find out if a 32 bit number is TMax? TMax is the maximum, two's complement number. My thoughts so far have been: int isTMax(int x) { int y = 0; x = ~x; y = x + x; return !y; } That is just one of the many things I have unsuccessfully have tried but I just cant think of a property of TMax that would give me TMax back. Like adding tmax to itself would be unique compared to all the other integers. Here is the actual problem: /* * isTMax - return 1 if x is the maximum, two's complement number, * and 0 return otherwise. * Legal ops: ! ~ & ^ | + * Max ops: 10 *

n is negative, positive or zero? return 1, 2, or 4

↘锁芯ラ 提交于 2019-12-09 07:26:55
问题 I'm building a PowerPC interpreter, and it works quite well. In the Power architecture the condition register CR0 (EFLAGS on x86) is updated on almost any instruction. It is set like this. The value of CR0 is 1, if the last result was negative, 2 if the last result was positive, 4 otherwise. My first naive method to interpret this is: if (n < 0) cr0 = 1 else if (n > 0) cr0 = 2; else cr0 = 4; However I understand that all those branches won't be optimal, being run millions of times per second.

How to find x mod 15 without using any Arithmetic Operations?

可紊 提交于 2019-12-09 06:57:06
问题 We are given a unsigned integer, suppose. And without using any arithmetic operators ie + - / * or % , we are to find x mod 15 . We may use binary bit manipulations. As far as I could go, I got this based on 2 points. a = a mod 15 = a mod 16 for a<15 Let a = x mod 15 then a = x - 15k (for some non-negative k ). ie a = x - 16k + k ... ie a mod 16 = ( x mod 16 + k mod 16 ) mod 16 ie a mod 15 = ( x mod 16 + k mod 16 ) mod 16 ie a = ( x mod 16 + k mod 16 ) mod 16 OK. Now to implement this. A