Questions tagged [bitwise-operators]

Operators which are used to perform manipulation at bit-level.The programming languages are Byte oriented whereas the hardware is bit oriented.Therefore, being able to operate on bit level can be very important in programming,especially when the program directly interact with the hardware.

Filter by
Sorted by
Tagged with
-1 votes
1 answer
39 views

how to perform bit wise "or" operation with same value/ variable?

I am trying to understand bitwise operation that took place in same variable within itself. let's say i have 2 variable x which is 4-bit reg type, if i write w_out<=(|x); you can take value of x as ...
superb ranjeet's user avatar
-7 votes
1 answer
73 views

Parsing of bits with XOR and Complement [closed]

I have a problem to locate the number which occurs only once when all other numbers in the vector occur exactly thrice. class Solution { public: int singleNumber(vector<int>& nums) { ...
Helena's user avatar
  • 418
-2 votes
0 answers
70 views

How do I create a bitmask of 26 bits to toggle based on characters in a string? [closed]

I would like to know how to create a 'bitmask' of a specific size n. For example, an idea I would like to try is to have a mask of 26 bits, in which I would set the bits to 0 or 1 depending on the use ...
Sterfry's user avatar
  • 11
0 votes
0 answers
67 views

Using bitwise to multiply by 5/8 function in C [duplicate]

I've been trying to figure out the error in my code but I can't seem to figure out what the error is. I've walked throguh each line and it makes sense. The goal is to multiply by 5/8ths rounding ...
Pnc_Gamba's user avatar
0 votes
1 answer
41 views

How to implement solidity bitwise in Golang

Code in UniswapV3 Solidity contract TickBitmap uint256 mask = ~((1 << bitPos) - 1); How to implement this solidity caculation in golang? I had founded some example code in Golang: a := new(...
Sylar's user avatar
  • 9
-1 votes
2 answers
112 views

Exercise about counting 1 bits in an integer in C

Exercise 2-9. In a two's complement number system, x &= (x-1) deletes the rightmost 1-bit in x. Explain why. Use this observation to write a faster version of bitcount. The bitcount function ...
oabdullae's user avatar
1 vote
1 answer
58 views

Extracting the Last N Bits from an Array of Bytes in C

I have an array of unsigned characters in C: unsigned char array[] = { 0xF0, 0xCC, 0xAA, 0xF0}; /* Represented as binary: 11110000 11001100 10101010 11110000 */ I'd like to extract the last N bits ...
Du6's user avatar
  • 45
2 votes
1 answer
78 views

Bitwise operation of a number goes wrong or not

Output of ~43 in c++ is -44 but I calculate that 212. In c++, I'm just calculating ~43 and this is my code: #include <iostream> using namespace std; int main(int argc, char *argv[]) { char ...
gamefal 1820's user avatar
1 vote
2 answers
66 views

Find the amount of integers from 0 to k that AND with x is equal to 0 [duplicate]

Given k and x, you have to find the amount of integers i, between 0 and k, such that i AND (bitwise &) x is equal to 0. 1 <= x <= 10^9 1 <= k <= 10^9 |k - x| <= 10^6 I need this to ...
Tomek Swiecki's user avatar
0 votes
2 answers
46 views

what will be the value of ~(true) in environment? and need answer justification

I came across a weird observation recently when I came across below piece of program. // Write C code here bool abc = true; bool cba = true; bool bac = ~abc; if(cba && bac) { printf(&...
user2706764's user avatar
1 vote
1 answer
23 views

Hamming Distance: unclear logic step involving bitwise operation

Solving some leetcode question I encountered the hamming distance problem (which I almost forget). the question was: Given two integers, return the Hamming distance between them. Now, I know and ...
LiorA's user avatar
  • 53
0 votes
1 answer
47 views

Nand2Tetris: How to code 16-bit constant in ALU

I'm doing ALU task in Project 2 in Nand2Tetris course. // and operates on the resulting values, as follows: // if (zx == 1) set x = 0 // 16-bit constant // if (nx == 1) set x = !x // ...
user243557's user avatar
0 votes
7 answers
225 views

C program return 1 if x > 0, 0 if x = 0, or -1 x < 0

i need to write a program that will return 1 if x > 0, 0 if x = 0, or -1, if x < 0 in C using only bitwise operators. Edit: sorry for any confusion since bitwise is what the prompt says but ...
Chandler Dotson's user avatar
-1 votes
2 answers
47 views

Nibble_Swap or Swapping 0x1234

The Input is 0x1234 and I want the output as 0x2143 but I am getting 0x4321 #include <stdio.h> #define SWAP(num) (((num) & 0x0F) << 4) | (((num) & 0xF0) >> 4) int main() { ...
Saikumar's user avatar
2 votes
1 answer
69 views

How do I speed up rotating a block of bits?

I'm writing a program that does the same thing in Rust and Haskell. It's a whole-message cipher; it reads in an entire file, encrypts it, and writes it out. Given 1 MiB, the Rust code takes 2.4 s, but ...
Pierre Abbat's user avatar
0 votes
1 answer
74 views

F# Bitwise operators and units of measure

When I define a unit of measure in my code, I can use the basic arithmetic operators on values with the unit without any problems, e.g. type [<Measure>] m let a = 1<m> let b = 2<m> (...
LA.27's user avatar
  • 1,898
30 votes
2 answers
2k views

Logical vs. bitwise operators. Both are wrong?

When I use the following minimal code in an C++ console application in Visual Studio 2019, I get two warnings, which are completely opposite. int main() { unsigned char op1 = 0x1; unsigned ...
woelfchen42's user avatar
0 votes
0 answers
18 views

Open Cv Python Code with bitwise operators for measuring mass of the green color

How do I check the mass of green colour using bitwise operators in opencv Python? I tried using the cv2.inRange function to create a mask that isolates the green colour in an image, and then ...
Jasgunh Singh's user avatar
0 votes
1 answer
58 views

What is the algorithm to count the number of 1 bits in a 64-bit integer? [duplicate]

For example: ./bit_count 0x123456789abcdef0 bit_count(0x123456789abcdef0) returned 32 What would be an algorithm to count the number of 1-bits in the 64-bit integer value that was passed in to the ...
Software Guy's user avatar
0 votes
2 answers
55 views

Why is 0xF used for bitwise operations involving conversions from BCD values to integers?

This may be a really dumb question, but why is 0xF used in the bitwise operations when converting from a BCD values to an integer. I understand that 0xF represents something like 0x15, which is equal ...
Software Guy's user avatar
2 votes
1 answer
120 views

Find the number of pairs of natural numbers from l to r which Bitwise AND is equal to 0

Given l and r, find the number of pairs of natural numbers from l to r which Bitwise AND is equal to 0. Limits : 1 <= l <= r <= 10^9 r - l <= 10^6 I could only write a brute force. Does ...
Tomek Swiecki's user avatar
0 votes
2 answers
68 views

What do I need to import to do bitwise operations?

I'm trying to use bitwise operators and I can't figure out what I need to import. $ cat tmp.hs import Data.Word import Data.Bits mask = 0xff :: Word64 v = 98213 :: Word64 lsb = mask & v And ...
azani's user avatar
  • 486
0 votes
1 answer
43 views

Converting back from bitwise yield wrong expected value (Solved)

I'm trying to convert back from a byte buffer to it corresponding ulong, but can't recreate the value... I must be blind or doing something wrong here :( ulong us = (ulong)(millisecondsSinceUnixEpoch *...
Tiago Conceição's user avatar
0 votes
1 answer
38 views

Print ith bit of the binary number, please find the error as it is printing wrong numbers on some bits [duplicate]

public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = 1010110; System.out.println("the ith bit is " + ...
Ashish Kumar's user avatar
-3 votes
1 answer
55 views

In real time project where we use shift operator and why?

I want explanation about in real time project where we use shift operator and why we use? in which type of project we use? detailed explanation from developers but not expecting default definition and ...
Kameshwar S SPK's user avatar
1 vote
0 answers
50 views

Compare two integers with bitwise operation

I saw a bunch of codes which can do simple arithmetic operations in constant-time. But there is a code makes me curious. Here is a code written in C. int ct_lt_u32(uint32_t x, uint32_t y){ return (...
Serenity's user avatar
2 votes
5 answers
178 views

a bitwise expression that returns 1 when mask is all zeros and x when mask is all ones

Let's say we have a mask variable that can either be 0 (all zoers) or -1 (all ones). I have another variable x which is let's say 42. What (possibly) is the bitwise expression that results in: 1 when ...
Ali's user avatar
  • 366
1 vote
1 answer
111 views

When does bit shifting cause undefined behavior in C++?

When does bit shifting cause undefined behavior in C++? And does the behavior differ between C++ versions and integer types?
palapapa's user avatar
  • 573
0 votes
1 answer
28 views

strange issue with PHP [closed]

I have been testing the bitwise operators in PHP and encountered a very strange issue. The following program <?php $foon = b'11111111'; for ($i = 0; $i <= 7 ; $i++) { $foo = (($foon &...
fat penguin's user avatar
2 votes
3 answers
89 views

what happens if we compare unsigned short int and unsigned char (comparing the bits)? (in C language)

For example: unsigned char mask1 = 0x55; //01010101 unsigned short int mask2 = 0x8055;//1000000001010101 unsigned short int res = 0; //0000000000000000 res = mask1 | mask2; so what ...
Pentakorr's user avatar
0 votes
1 answer
82 views

C# Bitwise operations Generic class

I am writing a c# bitwise operations class. I have the following enum public enum Fruits { Apple = 1, Orange= 2, Mango = 3, Grapes = 4, Pineapple = 5 } I need to store one or more flags for ...
Max's user avatar
  • 470
-3 votes
3 answers
115 views

What's the purpose of & in Java?

So, I was following a pacman tutorial and the coder uses & not &&. He doesn't explain what it does though. I searched it up and it says that unlike &&, & checks both ...
Smile D's user avatar
  • 33
1 vote
1 answer
135 views

Which Python method is more efficient for checking powers of 2?

Consider the following two Python implementations for checking if a number is a power of 2: Method 1: Using Math.log() import math def is_power_of_two(n): if n <= 0: return False v ...
george orwell's user avatar
-2 votes
1 answer
85 views

What is the purpose of '<<8 <<16 <<24' in this C++ code snippet for reading binary files? [closed]

I don't understand how these rows works, in particularly the expression << 8, << 16, << 24 while (1) { int32_t num = 0; num += is.get(); num += is.get() << 8; ...
Silviam's user avatar
  • 11
0 votes
1 answer
79 views

Get / Set only specific bits in a long

I have a 48 bit struct with several fields that I want to be able to read and manipulate. I think i got the getter part, but I am struggling with the setter. Here is some sample code: Question is, for ...
user1957413's user avatar
0 votes
0 answers
50 views

Recursive N-nested for loops with N assignments

I'm trying to do bitwise logical operations over a series of vectors. I think I need recursion, but I'm not sure how to get the recursion implemented and assign the values I need during recursion. ...
Yume's user avatar
  • 426
1 vote
1 answer
67 views

Bitwise not operator doesn't flip bits [duplicate]

I am trying to use the bitwise not operator to flip the bits in 1. I start with 1 (00000001) and add ~. x = ~1 print(f'{x:08b}') Result is -0000010. Everything online tells me the result should be ...
newbie54's user avatar
2 votes
4 answers
252 views

Is it possible to make x%10 (modulus 10) with bitwise operators in C?

I've been trying to search this specific answer here on SO and other websites, but I haven't got any clear answer so far. Is it possible to find the last digit of an integer as of x % 10 using bitwise ...
thevoyager's user avatar
2 votes
1 answer
88 views

How to separate binary sequence into periodic binary sequences

For example I want to separate this sequence 1 1 1 1 0 1 1 1 0 1 It can be done this way Example of separation where | is a binary OR. The first sequence has period 3 and offset 0, the second has ...
bio grisha's user avatar
1 vote
1 answer
86 views

Is there an easy way to merge several BitArrays?

I have several BitArrays that will need to be merged to into 48 byte structure. BitArray a1 = new (3); BitArray a2 = new (11); BitArray a3 = new (14); //And so on //Do bit manipulation, etc with the ...
Azriel H's user avatar
0 votes
1 answer
128 views

Is there a bitwise operation that returns 1 of the bits are equal?

I am trying to write a simple C bitwise operation that returns YES if the bits are equal. Truth table is the following: I tried various combinations of AND,XOR and OR and no luck. Granted, I could ...
affluentbarnburner's user avatar
0 votes
0 answers
39 views

Effect of bitwise AND while evaluating an boolean statement in Java, which is different than logical AND

So today I was doing some leetcode problems, and I came across this one: number of closed islands. This is my initial solution: class Solution { public int closedIsland(int[][] grid) { ...
zxcisnoias's user avatar
0 votes
2 answers
91 views

javascript problem with big numbers and bitwise operation

In my program I generate the code from the incoming number "int". Sometimes I need to do the reverse action, translate the code into a number. With small numbers, it works fine. const int = ...
smeninick's user avatar
0 votes
1 answer
34 views

Confusion in Variable type int and Byte during Left shift operator

In left Shift.... I choose all Variables type are Byte. But there is an error show about incompatible types. that is what i'm trying to do in Code
Hemant Singh Tanwar's user avatar
1 vote
3 answers
77 views

Cyclic number shift to the right byte by byte

The program takes two numbers as input: num - a number for which a right cyclic shift must be performed byte by byte, and bytes - the number of bytes by which it must be shifted to the right. The ...
nnn's user avatar
  • 13
0 votes
1 answer
56 views

Checking for multiple bitflags - binary comparison in [PHP]

Checking for multiple flags. I just want to know if there is another way to check for multiple flags, different from &&? class DemoClass { public const MY_FLAG1 = 0b1; // 0001 // 1 ...
DevWL's user avatar
  • 17.4k
0 votes
2 answers
152 views

Left Bit shift and casting

I have a behaviour that i don't understand, i try to construct an 64 integer from an array of bytes from big endian to little endian. uint64_t u; uint8_t bytes[2]; bytes[1] = 0xFF; u = bytes[1] <&...
mystackoverflowpseudo's user avatar
2 votes
3 answers
98 views

a |= (1 << 31) results in unexepected value

I made a reproducible code as below/ #include <stdio.h> int main(void) { long int a = 0x0; a |= (1 << 31); printf("a: 0x%lx\n", a); } I expect 'a' should be ...
Jason Park 's user avatar
1 vote
1 answer
46 views

bitwise concatenate and or difference

I have trouble understanding the difference between bitwise concatenate and or. For example, I have to bianry values which is 1111 and 0000. If I do 1111 | 0000, will I get 1111 or 11110000? And if I ...
Wen Sun's user avatar
  • 35
2 votes
2 answers
88 views

How can I force bitwise operators to produce unsigned results?

I'm working on a CRC32 algorithm in JavaScript, which I'm new to. Everything seems to go well until the last step: XOR the checksum with 0xFFFFFFFF. 1001010100111101101100010001000 ^ ...
Shadow_Guo's user avatar

1
2 3 4 5
61