Bit Packing: Squishing Booleans Using Bitwise Operations twin.sh/articles/54/bit-p…
πŸ‘︎ 70
πŸ’¬︎
πŸ‘€︎ u/TwinProduction
πŸ“…︎ Nov 27 2021
🚨︎ report
Is there an overflow bit of some sort for CUDA bitwise operations? (using Python)

I need to work with large integers and the standard 64 bit uint doesn't cut it. Using 2 of these and doing bitwise operations is easy enough, but I can't seem to figure out how to tell if my carry caused the number to overflow.

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/FwibbFwibb
πŸ“…︎ Jan 06 2022
🚨︎ report
As someone who'd never touched bitwise operations (or 95% of the other stuff in this game) before a few days ago, I'm incredibly proud of my solution to SIGNED LESS.
πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/Green_Gem_
πŸ“…︎ Dec 20 2021
🚨︎ report
Using bitwise operations with flags

I have an enum:

 enum mode {
      WALK = 1,
      JUMP = 2,
      WALK_JUMP = 3,
      RUN = 4,
      RUN_JUMP = 6
 };

Is there a way to write the below shorter using bitwise operators?

 if (mode == JUMP || mode == WALK_JUMP || mode == RUN_JUMP) {
      //
 }

Edit: Fixed errors in the code

πŸ‘︎ 6
πŸ’¬︎
πŸ“…︎ Nov 08 2021
🚨︎ report
Better syntax for bitwise operation on each column of a 2d bitarray, to form a combined 1d row.

reg [15:0] maskarr[0:7];

wire[15:0] mask;

would be something like

for each column, i, in maskarray:

bitwise OR all the elements in the column and put the result in mask[i]

I found the syntax

reg [15:0] maskarr;

wire mask = |maskarr;

but this doesn't seem to be able to be expanded to a 2d bitarray like so:

reg [15:0] maskarr[0:7];

wire[15:0] mask = |maskarr;

I can do

always

for (integer i = 0; i < 16; i=i+1)

mask[i] = |(maskarr[i]);

but I'm wondering if there's an easier cleaner way so that I do not need a for-loop.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/grobblefip746
πŸ“…︎ Nov 03 2021
🚨︎ report
Bitwise NOT operation doesn't work as expected on ESP32

If I write:

unsigned int var = 10;
Serial.println(~var);

On my ESP32, it converts the number into an unsigned long. If I use uint16_t it treats it as a signed int.

My solution was to cast it which worked.

Serial.println(uint16_t(~var)); 

This doesn't seem like intended behavior. Is there a place I can submit a bug report? And to who?

πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/BobStraitFTW
πŸ“…︎ Sep 29 2021
🚨︎ report
Bitwise Operations for Data Engineers confessionsofadataguy.com…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/dataengineerdude
πŸ“…︎ Oct 13 2021
🚨︎ report
Enums, and bitwise operations on enums in dotnet youtu.be/lHHgptSmtl0
πŸ‘︎ 42
πŸ’¬︎
πŸ‘€︎ u/cseigel
πŸ“…︎ Jun 15 2021
🚨︎ report
Why couldn't the pet store sell the genius ant that could do bitwise operations?

It was XOR-bit-ant-ly priced.

πŸ‘︎ 45
πŸ’¬︎
πŸ‘€︎ u/MrPancholi
πŸ“…︎ Jul 17 2021
🚨︎ report
how to reverse bitwise operation

so i have two values

95402AF9 and 6ABFD506

i can convert 6ABFD506 to 2504010489 to doing the following

crc= int("6ABFD506",16) ^ 0xFFFFFFFF

then

hex(crc) == 0x95402AF9

how do i do this the other way IE convert 95402AF9 into 6ABFD506

thanks

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/martynrbell
πŸ“…︎ Aug 20 2021
🚨︎ report
How are the bitwise shift(>> and <<) operations performed on negetive numbers?
#include&lt;stdio.h&gt;
int main()
{
    int i= -15;
    
    printf("%d\n",i&gt;&gt;1);                   
    printf("%d\n",i&gt;&gt;2);
    printf("%d\n",i&gt;&gt;3);
    printf("%d\n",i&gt;&gt;4);
    printf("%d\n",i&gt;&gt;5);
    return 0;                          
}

I got asked by my friend as how exactly does the Bitwise shift operator work on negetive numbers? As to how the MSB/Sign bit is preserved?

πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/ChayanDas19
πŸ“…︎ Jun 14 2021
🚨︎ report
Question regarding bitwise operations: calculating bit mask and bit shift values

I am supposed to determine the bit mask and shift values based on the following 3 values: the start byte, the start bit, and the number of bits.

I will receive some byte array for my data of x many bytes. Let's say some random chunk of data within this large byte array represents whether a light switch is on or off. So I will focus on this single byte/8-bits of data for this example. 3 bits out of these 8 bits are used to determine the on/off state of the light switch.

So, all I know about this chunk of data are these 3 fixed values: the starting byte position within this byte array, the starting bit position within this specific byte itself, and the number of bits allocated to represent the on/off state of this light switch.

So in this particular example, the byte position is byte 15 (within this large byte array of x many bytes). The starting bit position is 0. The number of bits is 3 (since 3 bits are allocated within this byte to represent on/off state).

My question is: how do I calculate the bit mask and bit shift value based on these 3 fixed values? I know the calculation might involve base 2 or raising to the power of 2, but unsure.

Thanks in advance

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/user49874
πŸ“…︎ Aug 04 2021
🚨︎ report
Best resource to learn BITWISE Operations in C++.

I have 0 knowledge of bitwise operations and I want to master it. I am looking for some resourcesπŸ“ to learnπŸ‘©β€πŸ’» bitwise operations form Beginner to Advanced.

πŸ‘︎ 15
πŸ’¬︎
πŸ‘€︎ u/xozov
πŸ“…︎ May 24 2021
🚨︎ report
Bashmash - Math utilities for Bash (update 0.3 - added support for bitwise operations) github.com/HubertPastyrza…
πŸ‘︎ 18
πŸ’¬︎
πŸ‘€︎ u/HubertPastyrzak
πŸ“…︎ Mar 15 2021
🚨︎ report
Bitwise operations in Java

Hi All,

can anyone recommend some good resources on Bitwise operations/Bitwise Manipulation/ Bit Hacks in Java Please

thanks

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/1122345566
πŸ“…︎ May 23 2021
🚨︎ report
Write functions for all the basic arthritic operations using just bitshifts and bitwise operations. \#stopcompilerabuse
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/IIAOPSW
πŸ“…︎ Apr 14 2021
🚨︎ report
Same operator for logical and bitwise operations.

Do you think it's a good idea to use the same operator for logical and bitwise operations?

1 &amp; 5 -&gt; 1 (bitwise)

true &amp; false -&gt; false (logical)

The same for | (or) ^ ( xor).

It sort of converges when using vectors.

{true, false, false} &amp; {true, true, false} -&gt; {true, false, false}

It only works if the language is strictly typed. Anything else I may miss?

Little nitpick is, ^ is now used, so power would need to be pow() or **.

Are there languages such syntax?

Edit: Thanks for the great inputs. It seems short-circuiting is the biggest concern. Functions in my language have no side effects. So I think it is less of an issue. (Short-circuit is in fact an optimization pass). My language has no pointers so the common "test for nullptr and dereference" does only seldom happen with optionals. Then it's just a sensible left to right evaluation.

πŸ‘︎ 49
πŸ’¬︎
πŸ‘€︎ u/danybittel
πŸ“…︎ Nov 05 2020
🚨︎ report
Need explanation on bitwise operations question/answer in C++

So, I came across a bitwise question where given integers n, p1 and p2, determine if the bits of position p1 and p2 are the same. Positions p1, p2 are 1 based with 1 being the least significant bit.

The concept of bitwise operation is new to me. So please correct me if I am wrong as I am still trying to get my head around this.

So firstly, when talking about least significant bit, it means reading bits from right to left, correct? Next, when talking about 1 based, it means starting count from 1 rather than 0, correct?

So, let's say n is 86, p1 is 2 and p2 is 3.

As 86 in binary number is 1010110, both p1 and p2 are the same, correct? Counting from left to right, the bit at their position is both 1.

Coding this:

I came across a solution for this to be bool same_bits = (((n &gt;&gt; p1-1) &amp; 1) == ((n &gt;&gt; p2-1) &amp; 1)). Now I understand the concept but not quite sure how it works. I get that I am comparing p1 bit and p2 bit but don't get how it's obtaining those bit through that code.

Could someone please explain this code to me?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/AaySquare
πŸ“…︎ Mar 14 2021
🚨︎ report
LPT: For people learning to code, make it a practice of adding brackets while using bitwise operations, you might not even realise where it go wrong, a simple bracket will make it clear.

More of a CPT coding pro tip hehe

πŸ‘︎ 20
πŸ’¬︎
πŸ“…︎ Apr 18 2021
🚨︎ report
Generally speaking, is it more efficient to use bitwise operations rather than conditional statements/operations?

As the title states, is it generally more efficient to use bitwise operations rather than conditional statements/operations (assuming both are optimized)?

As an example, I'm currently writing some code (in Java), and for a particular section, I want a value to be greater than or equal to 0 (if the value is negative, then treat it as 0).

Assuming n and y are both signed 32-bit integers, the following statements will complete the same task:
1.) y = (n &lt; 0 ? 0 : n);
2.) y = n &amp; ~(n &gt;&gt; 31);

Ignoring ease of readability, would statement 1 or 2 be better?

πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/cgjnm
πŸ“…︎ Oct 23 2020
🚨︎ report
Bit operations without bitwise operators

To account for XY problem the X question is as follows:

This is a task in structs and unions topic.

Write functions that do bit operations on 32-bit unsigned integer.

int toggle_bit(uint32_t* pvalue, int b);
int set_bit(uint32_t* pvalue, int b);
int clear_bit(uint32_t* pvalue, int b);
int isset_bit(const uint32_t* pvalue, int b);

Functions are to take pvalue pointer to a number and a bit number. They should return -1 in case of incorrect input and 0 on success with exception of function isset_bit which should return 0 or 1 if given bit is set or not. You may not use bit-wise operators, allocate memory or use [] operator except array declarations. Write a program that takes number, bit number and operation number (0 - toggle, 1 - set, 2 - clear, 3 - check if set) and then prints edited number in hexadecimal format (for operation 0-2) or 0/1 in decimal (for operation 3).

The Y question is:

I have written this functions that I think do work but I am really not satisfied with the solution. Pasting only toggle_bit function because the rest is very similar.

union bits_set {
    uint8_t bit_val;
    struct {
        uint8_t bit0 : 1;
        uint8_t bit1 : 1;
        uint8_t bit2 : 1;
        uint8_t bit3 : 1;
        uint8_t bit4 : 1;
        uint8_t bit5 : 1;
        uint8_t bit6 : 1;
        uint8_t bit7 : 1;
    } bits;
};

union bytes_set {
    uint32_t value;
    union bits_set bytes[4];
};

int toggle_bit(uint32_t* pvalue, int b)
{
    if (pvalue == NULL || b &lt; 0 || b &gt; 31) return -1;

    union bytes_set bytes;
    bytes.value = *pvalue;

    union bits_set *dst_byte = (bytes.bytes + b / 8);
    uint8_t dst_bit = b % 8;

    if      (dst_bit == 0) dst_byte-&gt;bits.bit0 = (dst_byte-&gt;bits.bit0 + 1) % 2;
    else if (dst_bit == 1) dst_byte-&gt;bits.bit1 = (dst_byte-&gt;bits.bit1 + 1) % 2;
    else if (dst_bit == 2) dst_byte-&gt;bits.bit2 = (dst_byte-&gt;bits.bit2 + 1) % 2;
    else if (dst_bit == 3) dst_byte-&gt;bits.bit3 = (dst_byte-&gt;bits.bit3 + 1) % 2;
    else if (dst_bit == 4) dst_byte-&gt;bits.bit4 = (dst_byte-&gt;bits.bit4 + 1) % 2;
    else if (dst_bit == 5) dst_byte-&gt;bits.bit5 = (dst_byte-&gt;bits.bit5 + 1) % 2;
    else if (dst_bit == 6) dst_byte-&gt;bits.bit6 = (dst_byte-&gt;bits.bit6 + 1) % 2;
    else if (dst_bit == 7) dst_byte-&gt;bits.bit7 = (dst_
... keep reading on reddit ➑

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/c_noob_1234
πŸ“…︎ Dec 09 2020
🚨︎ report
Trying to build a 4-bit CPU in No Man's Sky! (ALU input registers on the left, ALU on the right implementing 4-bit adder, AND, OR, NOT; bitwise operations)
πŸ‘︎ 492
πŸ’¬︎
πŸ‘€︎ u/TempledUX
πŸ“…︎ Mar 24 2020
🚨︎ report
Are bitwise operators in C used both for bit operations and for operations for integer or some C types?

In C,

  • Bitwise logical operators &amp;, |, ^ is used for selecting bits in a word.

  • Bitwise shifting operators &gt;&gt; and &lt;&lt; can be used for implementing multiplication and division between integers.

Questions:

  • Are bitwise logical operators also used for implementing operations on values of higher types (e.g. integers)?

  • Are bitwise shifting operators also used for bits operations?

Thanks.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/timlee126
πŸ“…︎ Oct 16 2020
🚨︎ report
Bitwise operations with less-than-helpful comments...
πŸ‘︎ 380
πŸ’¬︎
πŸ‘€︎ u/bbrk24
πŸ“…︎ Dec 27 2019
🚨︎ report
the article said that i had to runderstand bitwise operations
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/SauceSlayer
πŸ“…︎ Sep 14 2020
🚨︎ report
bitwise operation question
  1. The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
  2. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
  3. The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
  4. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
  5. The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
  6. The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it

https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/

enum test{
test_value_one = 1 &lt;&lt; 0
};

int l_flags = 0;
// set flag
l_flags |= test_value_one;
// unset 
l_flags &amp;= ~test_value_one;

// we can check if flag is set by
if(l_flags &amp; test_value_one){
/// flag is set
}
// or if not set
if(!(l_flags &amp; test_value_one)){
/// flag not set
}
//// is there are a other way to check if flag is not set?
/// why we can not do like  &amp;~ (AND NOT), or do we ? somehow I thing it will be faster then !( &amp;)
if(l_flags &amp;~ test_value_one){
/// flag not set ?
}
πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/zninja-bg
πŸ“…︎ Apr 18 2020
🚨︎ report
bitwise operations / bit flags?

Documentation and questions seem sparse

I am attempting something like this in gdscript:

# flags
const FLAG_NONE = 1 &lt;&lt; 0 
const FLAG_A= 1 &lt;&lt; 1
const FLAG_B = 1 &lt;&lt; 2

static func flag_is_enabled(b, flag):
        return b &amp; flag != 0

static func set_flag(b, flag):
        return b|flag

#static func unset_flag(b, flag):
#    return b&amp;^flag #  Error parsing expression, misplaced: '^'

This is one little thing driving me nuts because I'm not fully comfortable with bitwise operations to begin with and attempting to do this in gdscript while learning gdscript is frustrating. Is this correct? How to do &^ without causing an error? Any suggestions?

EDIT:

this appears functional

# flags
const FLAG_NONE = 1 &lt;&lt; 0 
const FLAG_A= 1 &lt;&lt; 1
const FLAG_B = 1 &lt;&lt; 2

static func flag_is_enabled(b, flag):
        return b &amp; flag != 0

static func set_flag(b, flag):
        b = b|flag
        return b

static func unset_flag(b, flag):
        b = b &amp; ~flag
        return b

kind of a pain but part of the learning process

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/blueblank
πŸ“…︎ Jun 17 2020
🚨︎ report
Bitwise operations will be the end of me
πŸ‘︎ 24
πŸ’¬︎
πŸ‘€︎ u/roljy
πŸ“…︎ Jun 12 2020
🚨︎ report
New Avx-512 instruction allows an 3-bit bitwise operation.

I was looking at the intel intrinsics guide, and I found a intrinsic for an instruction that takes in 3 zmm registers as input, and a imm8 that serves as a truth table. The instruction uses the truth table to perform any 3-bit operation that you want. For the ymm and zmm versions, the latency is reported as 1, and the reciprocal throughput is reported as 0.5. For the xmm version, the reciprocal throughput is lower at 0.33.

Intrinsic: _mm512_mask_ternarylogic_epi32

Instruction: vpternlogd zmm {k}, zmm, zmm, imm8

CPUID Flags: AVX512F

https://software.intel.com/sites/landingpage/IntrinsicsGuide/#techs=AVX_512&cats=Logical&expand=5865

πŸ‘︎ 21
πŸ’¬︎
πŸ‘€︎ u/_TheCoder_
πŸ“…︎ Jul 03 2020
🚨︎ report
Quick Question on bitwise operations

I'm a beginner programmer and I've been learning python on my own and also C as part of my college curriculum. I have stumbled upon Bitwise operators on both of the languages while reading and i understand how they work and all but I can't understand their purpose, how are they used in real life programs, and what do I benefit from operating on a bit level ?

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Doro_000
πŸ“…︎ May 20 2020
🚨︎ report
i love bitwise operations
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/boxxybab33
πŸ“…︎ Jul 08 2021
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.