Sponsored Content
Homework and Emergencies Emergency UNIX and Linux Support bitwise and between two 32 bit binaries Post 302577421 by hergp on Tuesday 29th of November 2011 04:15:59 AM
Old 11-29-2011
You can do something like this to split the value into bits:

Code:
typeset -i value=2#1110101010101011
typeset -i bit
typeset -i i=0

while (( value ))
do
    bit[i++]=$(( value & 1 ))
    (( value >>= 1 ))
done

for i in ${!bit[@]}
do
    printf "bit %2d = %d\n" $i ${bit[i]}
done

If you run this script you get

Code:
bit  0 = 1
bit  1 = 1
bit  2 = 0
bit  3 = 1
bit  4 = 0
bit  5 = 1
bit  6 = 0
bit  7 = 1
bit  8 = 0
bit  9 = 1
bit 10 = 0
bit 11 = 1
bit 12 = 0
bit 13 = 1
bit 14 = 1
bit 15 = 1

This works with bash and ksh93.
 

8 More Discussions You Might Find Interesting

1. Programming

Bit-fields and Bitwise operators

Hi, Is it possible to use bitwise operators in bit fields? For example: typedef struct Mystruct { unsigned char A :1 ; unsigned char B :1 ; } Mystruct; and assume struct Mystruct STR_1S, STR_2S, tempSTRS = {0}; then the following line: tempSTRS = STR_1S & STR_2S; gives the... (3 Replies)
Discussion started by: amatsaka
3 Replies

2. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

3. UNIX for Advanced & Expert Users

bitwise operators

can anybody write a program to divide a number by another number using bitwise operators (9 Replies)
Discussion started by: areef4u
9 Replies

4. Shell Programming and Scripting

Bitwise negation

I am taking an online course on Unix scripting. The topic is Unix arithmetic operators and the lesson is Logical and bitwise operations. It is not clear how much storage space Unix uses to represent integers that are typed. Bitwise negation caused me to question how many bits are used to... (3 Replies)
Discussion started by: dLloydm
3 Replies

5. Programming

bitwise and if

Hi Suppose we have these code lines: #define _IN_USE 0x001 /* set when process slot is in use */ #define _EXITING 0x002 /* set when exit is expected */ #define _REFRESHING 0x004 ... 1 main () { 2 3 unsigned r_flags =_REFRESHING; 4 5 if (r_flag &... (3 Replies)
Discussion started by: Puntino
3 Replies

6. FAQ Submission Queue

Analysis in bitwise XOR

The purpose of this article is revealing the unrevealed parts of the bitwise XOR. As we aware, the truth table for the XOR operator is : A B A^B 0 0 0 0 1 1 1 0 1 1 1 0 For example , 1^2 will be calculated as given below: First the operands... (1 Reply)
Discussion started by: pandeesh
1 Replies

7. Shell Programming and Scripting

how to use bitwise or operator in /bin/sh

please any one can suggest me how to use bitesie || opearator to do this #initallize a=0 b=0 #condition if then a=0 else a=1 fi #bitwise or opeartion b = a || b Please view this code tag video for how to use code tags when posting code and data. (3 Replies)
Discussion started by: Palaniappan
3 Replies

8. Shell Programming and Scripting

Bitwise comparison of cols

Hello, I want to compute the bitwise number of matches in pairwise fashion for all columns. The problem is I have 18486955 rows and 750 columns. Please help with code, I believe this will take a lot of time, is there a way of tracking progress? Input Org1 Org2 Org3 A A T A ... (9 Replies)
Discussion started by: ritakadm
9 Replies
atomic_bits(9F) 					   Kernel Functions for Drivers 					   atomic_bits(9F)

NAME
atomic_bits, atomic_set_long_excl, atomic_clear_long_excl - atomic set and clear bit operations SYNOPSIS
#include <sys/atomic.h> int atomic_set_long_excl(volatile ulong_t *target, uint_t bit); int atomic_clear_long_excl(volatile ulong_t *target, uint_t bit); DESCRIPTION
The atomic_set_long_excl() and atomic_clear_long_excl() functions perform an exclusive atomic bit set or clear operation on target. The value of bit specifies the number of the bit to be modified within target. Bits are numbered from zero to one less than the maximum number of bits in a long. If the value of bit falls outside of this range, the result of the operation is undefined. RETURN VALUES
The atomic_set_long_excl() and atomic_clear_long_excl() functions return 0 if bit was successfully set or cleared. They return -1 if bit was already set or cleared. ERRORS
No errors are defined. CONTEXT
These functions can be called from user, interrupt, or kernel context. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ SEE ALSO
atomic_add(9F), atomic_and(9F), atomic_cas(9F), atomic_dec(9F), atomic_inc(9F), atomic_or(9F), atomic_swap(9F), membar_ops(9F), attributes(5), atomic_ops(3C) SunOS 5.11 16 Jan 2006 atomic_bits(9F)
All times are GMT -4. The time now is 01:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy