Automaticly filling bits in c with f while doing ">>" operation

I am having some fun with bit operations in C and i don’t get one thing. If i have char b = 0x6c, and then i have short y = 0x0000, and I do y=b; y = y >> 12;, why does it fill my y with ‘f’? #include <stdio.h> #include <stdlib.h> int main() {… Read More Automaticly filling bits in c with f while doing ">>" operation

Displaying a message when a certain array element is present in C

This program displays certain messages when some of the elements are ‘y’ or if all of the elements are ‘n’. My question is about this line: someElements |= array[i] == ‘y’. I understand that it can be written in this way: someElements = someElements | array[i] == ‘y’. I’m just asking for an explanation why… Read More Displaying a message when a certain array element is present in C

*** stack smashing detected ***: terminated

#include <stdio.h> #include <stdlib.h> void get_nbits(int num, int n); void replace_nbits(int num, int n, int val); void get_nbits_from_pos(int num, int n, int pos); void replace_nbits_from_pos(int num, int n, int pos, int val); void toggle_bits_from_pos(int num, int n, int pos); void print_bits(unsigned int num, int n); int main() { printf("\tThis program is to show the below… Read More *** stack smashing detected ***: terminated

Bitwise operations on large numbers

This works as expected: > 0b1111 15 > 0b1111 & 0b1111 15 But this doesn’t: > 0b11111111111111111111111111111111 4294967295 > 0b11111111111111111111111111111111 & 0b11111111111111111111111111111111 -1 What’s going on here? And how can I safely perform bitwise operations on large (up to 2^32) numbers in Javascript? Update: This is below Number.MAX_SAFE_INTEGER so I don’t believe there should be… Read More Bitwise operations on large numbers

How to swap pairs of bits of unsigned int in C

This is the code I have so far? But its not working.. uint64_t bit_swap(uint64_t value) { return ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); } bit_swap(0x1111111111111111) should return 0x2222222222222222 but is returning 0x0000000022222222 instead >Solution : value & 0xAAAAAAAA That is equivalent to : value & 0x00000000AAAAAAAA And since we know… Read More How to swap pairs of bits of unsigned int in C